Question

I decided to start learning game programming and i chose cocos2d V3 as my first 2d game engine. As far as I understand, cocos2d-iphone v3 comes with chipmunk as the physics engine.

I manage to create a portrait application that has a Platform across left to right of the screen with an object (Box) on top of it.

Platform and Box are CCSprite with an attached CCPhysicsBody.

I wanted to push this box to the left when i touch the right side of the screen and push it to the right when i touch the left side of the screen. The box should not jump but just push left or right.

by looking from cocos2d v3 documentation, i found these methods : CCPhyicsBody

- (void)applyAngularImpulse:(CGFloat)impulse
- (void)applyForce:(CGPoint)force
- (void)applyForce:(CGPoint)force atLocalPoint:(CGPoint)point
- (void)applyForce:(CGPoint)force atWorldPoint:(CGPoint)point
- (void)applyImpulse:(CGPoint)impulse
- (void)applyImpulse:(CGPoint)impulse atLocalPoint:(CGPoint)point
- (void)applyImpulse:(CGPoint)impulse atWorldPoint:(CGPoint)point
- (void)applyTorque:(CGFloat)torque

and by using applyImpulse and passing the touch location CGPoint, i manage to make the box jump a little bit. But my requirement is for my box to just move to the left or right without jumping.

Can someone please explain to me which method should i use? and what should be the parameter for the method?

Thanks in advance for the help!

Was it helpful?

Solution

and by using applyImpulse and passing the touch location CGPoint, i manage to make the box jump a little bit.

This is the problem. Using applyImpulse is the right way to do it, however you need to provide the direction of the impulse as the CGPoint parameter. You do not want to use the touch location as impulse direction.

To apply an impulse only to the left you need to create a new CGPoint for the impulse direction and the y value of this point should be 0:

CGPoint impulse = ccp(-10.f, 0.f);

Then pass this point to the method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top