2014-01-30 76 views
0

我使用Cocos2D和Kobold2D制作游戏。在我的游戏,我有我想要移动到播放器水龙头,使用此代码的船舶:CCMoveTo引起跳跃

KKInput * input = [KKInput sharedInput]; 
CGPoint tap = [input locationOfAnyTouchInPhase:KKTouchPhaseBegan]; 
if (tap.x != 0 && tap.y != 0) 
{ 
    [ship stopAllActions]; // Nullifies previous actions 
    int addedx = tap.x - ship.position.x; 
    int addedy = tap.y - ship.position.y; 
    int squaredx = pow(addedx, 2); 
    int squaredy = pow(addedy, 2); 
    int addedSquares = squaredx + squaredy; 
    int distance = pow(addedSquares, 0.5); 
    [ship runAction: [CCMoveTo actionWithDuration:distance/100 position:tap]];//makes ship move at a constant speed 
} 

船一般移动,因为我希望它。但是,如果我在船的附近轻敲,而不是顺利地移动到水龙头位置,则会跳到该位置。我该如何解决?

+0

这是因为actionWithDuration的,您所提供的,在CCMoveTo行动。 – Renaissance

回答

0

你的逻辑是正确的,但是你的计算缺乏精确性。 特别是当您将飞行时间计算为距离/ 100时。这意味着当距离为< 100时,结果是0,所以船正在跳到目的地。

基本上,在处理cocos2d中的位置时,应该使用float而不是int。有几个功能可以很好地完成工作。

你可以改变你的代码如下:

[ship stopAllActions]; // Nullifies previous actions 
float distance = ccpDistance(tap, ship.position); 
[ship runAction: [CCMoveTo actionWithDuration:distance/100.0f position:tap]]; 
0

首先您可以使用cocos2d的ccpDistance函数来计算两点之间的距离。 它会跳跃,因为如果你的点击距离太近,太小假设2 or 10 ...你将它分开100 例如5/100 = 0.05这太低了,这就是它跳转的原因。 你需要手动处理它,我想有些东西像if(speed<1) speed++;加入1加快速度。快捷方式:)