Reputation: 69
I want to move my body continuos when i touch screen and stop when i release touch. I am using box2d and cocos2d and i really dont know why my code cant not perform very well. i an using touchesBegan for sprite body movement
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
ccTime dt;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
[self moveRight:dt];
// [self doWhateverYouWantToDo];
// [self doItWithATouch:touch];
}
-(void)moveRight:(ccTime)dt
{
CCSprite *ballright=(CCSprite *)ballbody->GetUserData();
NSLog(@"Ball PositionX: %f",ballbody->GetPosition().x);
NSLog(@"Ball PositionY: %f",ballbody->GetPosition().y);
[ballright runAction:[CCMoveTo actionWithDuration:1 position:ccp(ballbody->GetPosition().x,ballbody->GetPosition().y+5*dt)]];
}
So if its wrong than please tell me write logic and code Please Help me.
Thanks
Upvotes: 0
Views: 194
Reputation: 109
Apply LinearImpuls or LinearVelocity for movement of sprite Body in ccTouchBegan and in ccTouchEnd apply velocity to zero for stop sprite.
Upvotes: 1
Reputation: 64477
For continuous movement, CCMove* actions are not useful. At worst, if you create a new CCMove* action every frame, the object will effectively stop moving since there's a built-in 1 frame delay before movement starts.
Use and modify a velocity vector (CGPoint) and integrate it with position every frame to move the object.
Upvotes: 0