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

1 iphone objective-c cocos2d-iphone

我是Cocos2d的新手.我已经看过文档,看起来相比你需要使用基本的Iphone类(如UIAnimation等)做的相当简单.

我希望围绕圆周中心围绕圆周移动一个精灵(例如鸟,平面或汽车),以便即使精灵也相应地旋转.

这怎么可能在Cocos2d?如果有人可以发布一些基本代码,将会非常有帮助.

谢谢.

pgb*_*pgb 5

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

为此,您可以查看函数CGPointExtension.m.特别是,你可以使用ccpForAngle,ccpMultccpAdd.你可以这样做:

// 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
}
Run Code Online (Sandbox Code Playgroud)

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