0
\$\begingroup\$

In FlashPunk, you can have multiple animations set, and then choose which one to play at any time, and it'll do it.

Example: animatedSprite.add("running", [0, 1, 2, 3], 50); animatedSprite.add("falling", [4, 4, 4, 5, 1], 50); animatedSprite.play("running");

How do you accomplish this in libGDX?

I can only make it play one animation, looping, and I have no idea how to add multiple.

\$\endgroup\$
2
  • \$\begingroup\$ You can actually store the animation data in a texturepack. You have to name the images as they are sequenced. blog.isys-labs.com/swf-animation-in-libgdx-with-texturepacker \$\endgroup\$ – Sidar Jan 12 '14 at 2:49
  • \$\begingroup\$ Yes, but how do you choose between different animations? I can only get to constantly play, say, "walking". How do I make it so I can choose between "jumping" and "walking" and "running", etc? \$\endgroup\$ – CrateMuncher Jan 12 '14 at 11:51
2
\$\begingroup\$

I don't think you can do it out of the box with libGDX.

I don't know how you structure your game but personally i would simply load all the animations for a particular sprites has Animation instance and store them in a map where i associate a name to each one of them.

So basically:

Map<String, Animation> animations = new Map<>();
animations.put("walk", /*...*/);
animations.put("run", /*...*/);

And then just write a simple method that takes the name of animation, fetch the corresponding in the animations map and set this animation as the current animation.

public void play(String name) {
    this._currentAnimation = animations.get(name);
}

Then in your rendering method, you just need to refer to the _currentAnimation field of your Sprite class or whatever and play this animation as explained here.

EDIT: libGDX provides create tools for game development but sometimes you'll need to write some higher level code that wraps libGDX concepts to do what you want. libGDX is not a game engine, it's a framework, this way, it provides a lot of flexibility on how you want to implement things.

\$\endgroup\$
1
  • \$\begingroup\$ I hadn't even thought of that! Great idea. \$\endgroup\$ – CrateMuncher Jan 12 '14 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.