Search code examples
iphonecocos2d-iphonebox2d-iphone

Body Moves continuos when tap on screen


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


Solution

  • Apply LinearImpuls or LinearVelocity for movement of sprite Body in ccTouchBegan and in ccTouchEnd apply velocity to zero for stop sprite.