在Cocos2d-iPhone中围绕圆周移动精灵

时间:2010-05-07 07:05:18

标签: iphone objective-c cocos2d-iphone

我是Cocos2d的新手。我已经看了一下文档,看起来相比你需要使用UIAnimation等基本Iphone类所需要做的很简单。

我希望将一个Sprite(例如Bird,Plane或Car)围绕其圆周的圆心移动,平滑,以便即使精灵也能相应地旋转。

在Cocos2d中这怎么可能?如果有人可以发布一些基本代码,那将非常有用。

感谢。

1 个答案:

答案 0 :(得分:5)

一种方法是安排一个选择器定期运行,当它被调用时,将你的精灵移到圆圈上。

为此,您可以查看CGPointExtension.m处的功能。特别是,您可以使用ccpForAngleccpMultccpAdd。你可以这样做:

// Assume the class is a subclass of CCNode, and has the following properties:
// radius: the radius of the circle you want the sprite to move over.
// circleCenter: the center of the circle
// currentAngle: the angle where the sprite currently is
- (void)tick {
    float anglePerTick = 0.1; // this will determine your speed
    currentAngle += anglePerTick;  
    self.position = ccpAdd(ccpMult(ccpForAngle(currentAngle), radius)),
                           circleCenter);
    self.rotation = currentAngle * 180 / M_PI; // Convert from radians to degrees
}

这种方法的主要问题是你将角速度设置为常数,所以如果圆圈变大,精灵在每个刻度上行进的“距离”将增加,并可能导致闪烁。