0

Am a newbie in using sprite builder and cocos2d.

I Have done the sprite continuos running using sprite builder and its timeline. i.e Hero sprite will run continuously once app launches like temple run game.

Here this sprite should jump if user taps the screen. Am struct with implementing this how to make my running sprite to jump on tap on screen ?

Here below is the code which I written inside touchBegan for make my sprite to jump. Here I need to merge this running animation with sprite jump animation. Any suggestion and help will be greatly helpful.

    [_hero.physicsBody applyImpulse:ccp(0, 400.f)];
nik
  • 2,289
  • 6
  • 37
  • 60
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – CodeSmile Jun 20 '14 at 11:24
  • How to merge running sprite with jumping sprite upon screen touch – nik Jun 20 '14 at 13:52

1 Answers1

0

The way I did it is use a typedef enum to set up a set of variables that will store the character state. Set this up in a Definitions.h class. Then, in your hero class, add a method that will use a switch statement to change the state. For example, if jumpState is passed in and current state is runState, the method will change the state to jumpState and run a method called jumpAnim. You can then code your animation in JumpAnim or load it from spritebuilder.

Then, in another class where your Gameplay is finalized, call it a GamePlay layer, that is where your touchBegan method would be. In GameplayLayer.h, set up set up a CCTime variable to store the value of your jumptimer, how long your upward jump animation runs.

In your touchBegan method, you can change the state to jumping and reset the jumpTimer.

Then, in your update method, call a updateHero method. updateHero will have an if statement- if the hero state is jumpState, then move him up by 3 points until the jumpTimer runs out. Once the timer runs out, change the state to fallingState. Falling state should also be in the switch statement in your character class.

Hope this helps