Iphone CCSprite跟随一系列CGPoints
我正在开发一个线条绘制游戏,类似于飞行控制、海港大师和应用商店中的其他游戏,使用Cocos2D 对于这个游戏,我需要一个CCSprite来跟随用户画的线。我正在将一系列Iphone CCSprite跟随一系列CGPoints,iphone,cocos2d-iphone,Iphone,Cocos2d Iphone,我正在开发一个线条绘制游戏,类似于飞行控制、海港大师和应用商店中的其他游戏,使用Cocos2D 对于这个游戏,我需要一个CCSprite来跟随用户画的线。我正在将一系列CGPoint结构存储在NSArray中,基于我在touchsbegin和touchsmoved消息中获得的点。我现在的问题是如何让我的精灵跟随他们 我有一个tick方法,它以帧速率调用。在这种滴答声方法中,基于精灵的速度和当前位置,我需要计算它的下一个位置。有没有标准的方法来实现这一点 我目前的方法是计算最后一个“参考点”和下一
CGPoint
结构存储在NSArray
中,基于我在touchsbegin
和touchsmoved
消息中获得的点。我现在的问题是如何让我的精灵跟随他们
我有一个tick方法,它以帧速率调用。在这种滴答声方法中,基于精灵的速度和当前位置,我需要计算它的下一个位置。有没有标准的方法来实现这一点
我目前的方法是计算最后一个“参考点”和下一个“参考点”之间的直线,并计算该直线上的下一个点。我的问题是当精灵“转动”(从一段线移动到另一段线)时
任何提示都将不胜感激。为什么要编写自己的勾号方法?为什么不使用内置的
CCMoveTo
方法呢
(void) gotoNextWayPoint {
// You would need to code these functions:
CGPoint point1 = [self popCurrentWayPoint];
CGPoint point2 = [self getCurrentWayPoint];
// Calculate distance from last way point to next way point
CGFloat dx = point2.x - point1.x;
CGFloat dy = point2.y - point1.y;
float distance = sqrt( dx*dx + dy*dy );
// Calculate angle of segment
float angle = atan2(dy, dx);
// Rotate sprite to angle of next segment
// You could also do this as part of the sequence (or CCSpawn actually) below
// gradually as it approaches the next way point, but you would need the
// angle of the line between the next and next next way point
[mySprite setRotation: angle];
CCTime segmentDuration = distance/speed;
// Animate this segment, and afterward, call this function again
CCAction *myAction = [CCSequence actions:
[CCMoveTo actionWithDuration: segmentDuration position: nextWayPoint],
[CCCallFunc actionWithTarget: self selector: @selector(gotoNextWayPoint)],
nil];
[mySprite runAction: myAction];
}