Skip to content

Animation Manger Test

Raghuram Iyer "Ragzzy-R" edited this page May 19, 2014 · 14 revisions

This article tells how to use Animation Manager to maintain states.Most of them are easy to understand if you just take a look at AnimationManagerTest.java

walkAnimator = new Animator(1,7);
slideAnimator= new Animator(1,3);
player = new AnimatableEntity(1);

we just initialize our animators with rows and columns as parameters. This rows and columns are derived from the rows and columns of the spritesheet being used. As you can argue not all spritesheets are exactly having row and columns.(especially the one's made with some automatic tools). You can use another constructor which takes one argument (i.e) no. of frames. The below code is equivalent to the above code

walkAnimator = new Animator(1,7);
slideAnimator= new Animator(1,3);
player = new AnimatableEntity(1);

The AnimatableEntity is Also initialized.It takes one parameter that gives the minimum number of animators it will be attached with.

walkAnimator.addFrameDimesion(0, 0,   0, 70, 64);
walkAnimator.addFrameDimesion(1, 82,  0, 70, 64);
walkAnimator.addFrameDimesion(2, 152, 0, 70, 64);
walkAnimator.addFrameDimesion(3, 236, 0, 70, 64);
walkAnimator.addFrameDimesion(4, 312, 0, 70, 64);
walkAnimator.addFrameDimesion(5, 382, 0, 70, 64);
walkAnimator.addFrameDimesion(6, 457, 0, 70, 64);

The above lines are the only boring part. We need to add each frame's dimension, one by one. Which is lame actually. But this is the best I could think of.If anyone is ready to provide a better solution I'd be more than happy.

The parameters of that function are _index,x,y,width and height_ in that order.

Again the above function calls are needed only if your graphic artist is retarded and draws sprites randomly over the canvas or you just use a ripped sprite from google. If the sprite sheet is well organized in the form of matrix with each frames with same height and width then you can use this.

void setAllFramesDimension(int width,int height);  //call once
void addFrameCoOrdinates(int frameIndex,int x,int y);   //call for each frame

do the same for All animators.

then finally

player.attachAnimator(walkAnimator, "walk");
player.attachAnimator(slideAnimator, "slide");

attach all the Animators to the animatable entity.The first parameter of attachAnimator() takes the Animator and the second takes an ID of type String. Some may argue why not use int instead of String. The reason is, well strings are more readable than ints easy to remember. Secondly, The AnimatableEntity internally uses a HashMap that maintains all of its Animators. So its logical to use a string for a key rather than an int.Thirdly, Assuming that libgdx will turn out to be a full fledged engine with scripting support, it is good to use strings in scripts rather than ints, as scripting and strings go together.

finally

player.setCurrentAnimator("walk");`

thats it.this sets the currentAnimator.

so simply when you want to transition from one state to another, do something like this

if(jump) {

    player.setCurrentAnimator("jump");
}
if(somecondition) {
     player.setCurrentAnimator(ID);
}

Thats it.If you have any queries hit me up. Any comments and suggestions are welcome.

Thanks

Clone this wiki locally