Saturday, September 2, 2017

What's the best way to create animations when doing Android development?


I'm trying to create my first Android game and I'm currently trying to figure out (with someone that will do the drawings and another programmer) what the best way to create animation is. (Animations such as a character moving, etc.)


At first, the designer said that she could draw objects/characters and animate them with flash so she didn't have to draw every single frame of an action. The other programmer and I don't know Flash too much so I suggested extracting all the images from the Flash animation and making them appear one after the other when the animation is to start.


He said that would end up taking too much resource on the CPU and I tend to agree, but I don't really see how we're supposed to make smooth animations without it being too hard on the hardware and, if possible, not have the designer draw every single frame on Adobe Illustrator.


Can an experienced Android game developper help me balance this out so we can move on to other parts of the game as I have no idea what the best way to create animations is.




Answer



If you're considering 2D animation, rendering one sprite and then another will not be a problem for the device (unless your sprites are huge).


Below is a typical animation sprite that is the first one I found with a little searching. This is a commonly used technique and there are lots of websites that offer free (and otherwise, always check the license) spritesheets.


Image source: [here](http://gaminggroundzero.com/?page_id=31)


Image + author source: here


The green is set as the transparent colour of the image so that you don't have any unwanted areas of colour. You will need to know the coordinates of each animation in the animation sequence in the spritesheet in order to play them.


For Android, we can take a look at an open source game engine by Chris Pruett (formerly of Google) called Replica Island. The source code is available, and we can look at his SpriteAnimation.java and SpriteComponent.java.


A SpriteAnimation is simply created with a frame count and a unique ID and each actor in the game can have multiple animations. The SpriteAnimation can be given a number of AnimationFrames, each with a duration in milliseconds. This means that different animations can be swapped in and out depending on if the character moves or attacks etc (see in AnimationComponent.playAnimation(int id)).


Each iteration of the game loop, the AnimationFrame's getFrame(float time) is called, and it checks how much time has passed since the last main loop iteration and decides which frame to play next, then returns the frame to get sent off to the rendering system. This allows it to skip animation frames if the animation is going too slowly.


I hope this example works, and you can see it being used in the actual game on the Android Marketplace here.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...