I would like to jump up once and then land even when the jump button is pressed continuously.
Currently I am doing something like this.
When the jump button is pressed a flag is set indicating that the player needs to jump . The update loop then makes the character jump.When the jump button is released the character lands back down.Here is the code
void Player_Controls::onTouchesBegan(const std::vector<cocos2d::Touch*>& touch , cocos2d::Event* event)
{
....
else if(JumpButton->boundingBox().containsPoint(pA))
{
CCLOG("JumpButton Pressed");
//Tell the update loop to make this sprite jump
this->sprite_motion.motionDirection=SpriteMotion::MotionDirection::EnumJump;
}
}
//Set a flag when player releases the button
void Player_Controls::onTouchesEnded(const std::vector<cocos2d::Touch*>& touch , cocos2d::Event* event)
{
....
....
if(JumpButton->boundingBox().containsPoint(pA))
{
CCLOG("FireButton Released");
this->sprite_motion.motionDirection=SpriteMotion::MotionDirection::EnumFall; //Tell the loop to make character fall down
}
}
This is what goes in the update loop
if( this->sprite_motion.motionDirection == SpriteMotion::MotionDirection::EnumJump)
{
eagle->getPhysicsBody()->setVelocity(Vec2(0,400));
this->sprite_motion.motionDirection=SpriteMotion::MotionDirection::EnumNone;
}
if( this->sprite_motion.motionDirection == SpriteMotion::MotionDirection::EnumFall)
{
CCLOG("Falling Down");
eagle->getPhysicsBody()->setVelocity(Vec2(0,-400));
this->sprite_motion.motionDirection=SpriteMotion::MotionDirection::EnumNone;
}
Now the problem with this approach occurs if the jump button is continuously pressed and not released. The character keeps on going higher and higher. I wanted to know how to emulate a single jump even if the jump button is pressed continuously.
