1
\$\begingroup\$

Hey i have issue regarding jump sprite body. In my code i am using moveLeft and moveRight Button and when i am press moveRight Button using following code

if (moveRight.active==YES) 
    {
            b2Vec2 force=b2Vec2(4,0);
            ballBody->SetLinearVelocity(force);                
    }

Its move perfectly and When i release this Button than sprite body stop using following code

else
{
      b2Vec2 force=b2Vec2(0,0);
      ballBody->SetLinearVelocity(force); 
} 

But when i put this else part then jump can not done. My jump code is following

if (jumpSprite.active==YES) 
                {
                    NSLog(@"Jump Sprite");
                    b2Vec2 locationWorld;
                    locationWorld=b2Vec2(0.0f,4.0f);
                    double force=ballBody->GetMass();
                    ballBody->ApplyLinearImpulse(force*locationWorld, ballBody->GetWorldCenter());
                }

If i remove else part then jump will perform complete but sprite body can not stop after release button.

So what to do??

Thanks in advance

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Have you tried something like:

if (moveRight.active==YES) 
{
      b2Vec2 force=b2Vec2(4,ballBody.yForce);
      ballBody->SetLinearVelocity(force);                
}

if(moveRight.active==NO)
{
      b2Vec2 force=b2Vec2(0,ballBody.yForce);
      ballBody->SetLinearVelocity(force); 
} 

Note, I haven't used cocos2D, so I don't know how to properly get the y force (so I use ballBody.yForce, replace that with the correct code). I do recommend using the existing y force there, unless you want your jump to stop from x movement (same goes for your jump). I also find it strange that applying a force of 0 stops the ball, but you said that works.

\$\endgroup\$

You must log in to answer this question.

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