Vector 如何检测CCSprite何时达到某个点?

Vector 如何检测CCSprite何时达到某个点?,vector,cocos2d-iphone,distance,Vector,Cocos2d Iphone,Distance,我正在使用iOS的Cocos2d库编写一个游戏。到目前为止,我在屏幕上有一个精灵,我想拖来拖去。基本上我有一个CG点叫做End。当我在屏幕上拖动时,它会移动。我想让精灵在每一帧上计算出端点是否移动,以及它是否开始以给定的速度向它移动,然后停在它的正上方。结局就像锚。我通过如下绘制向量完成了前两个步骤: -(void) update:(ccTime)deltaTime { CGPoint Pos = _player.position; velocity = 15; dif

我正在使用iOS的Cocos2d库编写一个游戏。到目前为止,我在屏幕上有一个精灵,我想拖来拖去。基本上我有一个CG点叫做End。当我在屏幕上拖动时,它会移动。我想让精灵在每一帧上计算出端点是否移动,以及它是否开始以给定的速度向它移动,然后停在它的正上方。结局就像锚。我通过如下绘制向量完成了前两个步骤:

-(void) update:(ccTime)deltaTime
{
    CGPoint Pos = _player.position;
    velocity = 15;

    diff.x = End.x - _player.position.x;
    diff.y = End.y - _player.position.y;
    length = ccpLength(diff);

    norm.x = diff.x / length * velocity;
    norm.y = diff.y / length * velocity;

    Pos.x += norm.x;
    Pos.y += norm.y;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

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

CGPoint diff = ccpSub(touchLocation, oldTouchLocation);

End = ccpAdd(End, diff);
}
我像这样移动端点:

-(void) update:(ccTime)deltaTime
{
    CGPoint Pos = _player.position;
    velocity = 15;

    diff.x = End.x - _player.position.x;
    diff.y = End.y - _player.position.y;
    length = ccpLength(diff);

    norm.x = diff.x / length * velocity;
    norm.y = diff.y / length * velocity;

    Pos.x += norm.x;
    Pos.y += norm.y;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

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

CGPoint diff = ccpSub(touchLocation, oldTouchLocation);

End = ccpAdd(End, diff);
}

检测玩家何时到达目的地的最佳方法是什么?我试过很多种方法,但我真的无法做到足够精确。我尝试添加一个计时器来计时每次移动的持续时间,这样我就可以测试速度*持续时间>=length。这样做吗?结果不太好。有没有高级程序员想给我一些提示?

在更新方法的末尾,检查距离。如果“距离”低于“速度”,则完成操作,将位置设置为端点,然后使用任何方法触发。在cocos2d中,可以使用方法
ccpdance