如何使用CCTouchMoved在cocos2d中以速度/速度拖动CCSprite?

时间:2012-03-27 04:24:05

标签: ios cocos2d-iphone ccsprite

我有一个精灵,我想通过用手指在屏幕上移动来移动它〜拖动。 我希望我的精灵以速度移动,这意味着不要像我的手指移动一样快。

看起来像这个视频:http://www.youtube.com/watch?v=Vair3CIxZEw(从0:12到0:53)

这是我的ccTouch代码。我怎样才能让它移动看起来更顺畅?

谢谢!!! :)

只需返回TRUE

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

-(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;
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用CCMoveTo操作来平稳移动

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];
}