Mykod
Mykod

Reputation: 687

Sprite does not jump when moving

I'm making a platform game, and everything was great. My sprite moved when i touched the left side of the screen, and jumped when i touched the right side. But then i decided to make it move by itself. so i added the ccmoveto function, but now it does not jump! I'm new to cocos2d but everything is working ok, except this, already searched but couldn't find the answer can someone please help me? I tried everything, but it only jumps if i delete the ccmoveto action. I'm using cocos2d 2.0 Thank you!!

Upvotes: 1

Views: 134

Answers (2)

CodeSmile
CodeSmile

Reputation: 64477

CcMoveTo will override any manual position changes, inluding changes from other actions like CCJump. Your character is set to move to destination in a straight line, no matter what.

It's issues like these why I always recommend not to use actions for gameplay logic. Especially position, you need to retain full control over it. Use a direction vector and integrate position every update: and you're free to do everything you need.

Upvotes: 1

Andrea Sindico
Andrea Sindico

Reputation: 7440

my advice is to use one of the physics engines provided with cocos2d: Box2D and Chipmunk physics. Thanks to this engines you can define the characteristics of the world (i.e. gravity vector) a shape and a mass for your sprite (i.e. a rectangle with a weight). Then when you need it to jump you will just create a force vector with the characteristics you need (i.e. angle, etc.) and keep updated your sprite with its physical body. This will make your sprite jump and land quite realistically.

Upvotes: 0

Related Questions