2011-08-26 40 views
0

我正在使用iOS的Cocos2d库编写游戏。到目前为止,我在屏幕上有一个精灵,我想拖动它。基本上我有一个叫做End的CGPoint。当我拖过屏幕时,它会移动。我想让每个框架上的精灵都知道End是否已经移动,并且是否已经开始以给定的速度朝它移动,并且在它之上停下来。 End就像一个锚点。如何检测CCSprite何时到达特定点?

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

什么是检测的最佳方式时_player已经达到它的目的地?我已经尝试了很多方法,但我确实无法做到这一点。我试着添加一个计时器来计算每次移动的持续时间,所以我可以测试如果速度*持续时间> =长度。这是做到这一点的方式吗?它不太好。任何主程序员都想给我一些提示?

回答

1

在更新方法结束时,检查距离。如果距离低于速度,则表示完成,将位置设置为端点并根据需要启动任何方法。在cocos2d中,您可以使用方法ccpDistance

相关问题