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

在Cocos2d iPhone中围绕圆的圆周移动精灵,iphone,objective-c,cocos2d-iphone,Iphone,Objective C,Cocos2d Iphone,我是Cocos2d的新手。我看了一下文档,与使用基本的Iphone类(如UIAnimation等)相比,它看起来非常简单 我想把一个精灵(如鸟、飞机或汽车)绕着圆周的圆心移动,这样即使精灵也会相应地旋转 在Cocos2d中如何实现这一点?如果有人能发布一些基本的代码,这将是非常有帮助的 谢谢。一种方法是安排一个选择器定期运行,当它被调用时,将您的精灵移动到圆圈上 为此,您可以查看CGPointExtension.m中的函数。特别是,您可以使用ccpForAngle、ccpMult和ccpAdd。

我是Cocos2d的新手。我看了一下文档,与使用基本的Iphone类(如UIAnimation等)相比,它看起来非常简单

我想把一个精灵(如鸟、飞机或汽车)绕着圆周的圆心移动,这样即使精灵也会相应地旋转

在Cocos2d中如何实现这一点?如果有人能发布一些基本的代码,这将是非常有帮助的


谢谢。

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

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

// 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
}

这种方法的主要问题是将角速度设置为常量,因此如果圆变大,精灵在每个刻度上移动的“距离”将增加,并可能导致闪烁。

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

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

// 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
}

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

我将CCSprite子类化,并按照您所说的那样定期调用刻度,精灵将围绕半径移动,但是它没有相应地旋转,我是否缺少什么?您可以更改旋转属性。我只是更新了代码以反映它。我尝试了这个,但是旋转发生得非常慢,不是根据移动速度。我会添加我的代码,所以请告诉我我在做什么wrong@pgb:请告诉我哪里出了问题,我添加了代码作为答案。旋转属性以度为单位,而不是弧度,因此您需要转换它。我已将我的代码示例更新为
self.rotation=currentAngle*180/M\u PI。顺便说一句,你应该更新你的问题,而不是发布额外的答案,这样在网站上找到你的问题的访问者就不会那么困惑了。我将CCSprite分为子类,按照你说的做了,并定期调用tick,sprite会绕半径移动,但不会相应地旋转,我遗漏了什么吗?您可以更改旋转属性。我只是更新了代码以反映它。我尝试了这个,但是旋转发生得非常慢,不是根据移动速度。我会添加我的代码,所以请告诉我我在做什么wrong@pgb:请告诉我哪里出了问题,我添加了代码作为答案。旋转属性以度为单位,而不是弧度,因此您需要转换它。我已将我的代码示例更新为
self.rotation=currentAngle*180/M\u PI。顺便说一句,你应该更新你的问题,而不是发布额外的答案,这样在网站上找到你的问题的访问者就不会那么困惑了。