o0oKodako0o
o0oKodako0o

Reputation: 177

How can i drag a CCSprite with velocity/speed in cocos2d by using CCTouchMoved?

I have an sprite, i want to move it by using my finger to move around the screen ~ drag. I want my sprite move with velocity, it means do not as fast as my finger move.

it seem like this video: http://www.youtube.com/watch?v=Vair3CIxZEw (from 0:12 to 0:53)

Here is my ccTouch code. How can i fix to make it move look smoother?

Thank you!!! :)

simply return TRUE

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    return TRUE;  
}

and

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];


    CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
    if (CGRectContainsPoint(_car.boundingBox, touchLocation)) {            
        CGPoint newPos = ccpAdd(_car.position, translation);
        _car.position = newPos;
    }
}

Upvotes: 1

Views: 1056

Answers (1)

Geo Paul
Geo Paul

Reputation: 1817

Try using CCMoveTo action for smooth moving

CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
if (CGRectContainsPoint(_car.boundingBox, touchLocation)) {
    CGPoint newPos = ccpAdd(_car.position, translation);
    id moveAction = [CCMoveTo actionWithDuration:0.5f position:newPos];
    [_car runAction:moveAction];
}

Upvotes: 1

Related Questions