I'm developing a 2D platformer game using the Cocos2d-x Engine, i can load correctly the animations according in which action the player is(running, jumping, standing, etc).

Now i'm trying to add the attack action to the player, and i need some advice about the best way to handle the corresponding animation for this new action.

The first thing that comes to my mind is to create new states for the player and his corresponding animation. For example, JumpRightAttack, JumpLeftAttack, and so on. But handling this that way seems to be a mess, and if i have more than one weapon it will become even more messy.

On the other hand, i can have only one attack animation and attach the current weapon, but i'm a little lost of how to attach the weapon in the correct spot(i.e. player's hand)

Which method would you recommend??

  • 1
    \$\begingroup\$ I believe ppl usually make an animator class and then attach all possible animations to that object, so you can do player.animator.animate("attack1"); when the user presses the attack button (if he has the weapon). But you all animations need to know where, relative to the player, should they be displayed. \$\endgroup\$
    – zoran404
    Commented Jun 22, 2015 at 18:33
  • \$\begingroup\$ Check out the state pattern. Might be what you want for both logic and animation. \$\endgroup\$
    – lozzajp
    Commented Jan 3, 2017 at 15:53

1 Answer 1


Rather than using entire sprite sheet animation, I would recommend a 2D skeletal approach. Tools like Spine can help you here and are supported by cocos2d-x framework. The class in question is the SkeletonAnimation object.

Here is a sample code on how to initialize the same

source- cpp tests from cocos2d-x

//initilize the object with animation atlas
skeletonNode = SkeletonAnimation::createWithFile("spine/spineboy.json", "spine/spineboy.atlas", 0.6f);
//Setting the animation to play
skeletonNode->setAnimation(0, "walk", true);

This is just the bare bones code, for more detail look up the SpineTest.h/.cpp files in the cocos2d-x cpp-test folder.

This also helps you to "attach" the weapon to the Sprite's hands by adding an anchor point at the arms and then animating it from the Spine application. This also makes the code a bit more clean and reduces you sprite sheet size considerably.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .