问题描述
我是 Cocos2d 的新手.我查看了文档,与使用基本的 Iphone 类(如 UIAnimation 等)相比,它看起来非常简单.
I am new to Cocos2d. I have taken a look at the documentation at it looks pretty simple compared to what you need to do using basic Iphone classes such as UIAnimation etc.
我想围绕其圆周的圆心平滑移动精灵(例如鸟、飞机或汽车),以便精灵也相应地旋转.
I want to move a Sprite (eg. Bird, Plane or Car) around the center of a circle around its circumference, smoothly so that even the sprite rotates accordingly.
这在 Cocos2d 中怎么可能?如果有人可以发布一些基本代码,那将非常有帮助.
How is this possible in Cocos2d? It would be very helpful if someone can post some basic code.
谢谢.
推荐答案
一种方法是安排一个选择器定期运行,当它被调用时,将你的精灵移动到圆圈上.
One way to do it is to schedule a selector to run periodically, and when it's called, move your sprite over the circle.
为此,您可以查看 CGPointExtension.m
中的函数.特别是,您可以使用 ccpForAngle
、ccpMult
和 ccpAdd
.你可以这样做:
To do that, you can take a look at the functions at CGPointExtension.m
. In particular, you can use ccpForAngle
, ccpMult
, and ccpAdd
. You could do something like this:
// 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
}
这种方法的主要问题是您将角速度设置为常数,因此如果圆圈变大,精灵将在每个刻度上移动的距离"会增加,并可能导致闪烁.
The main problem with this approach is that you are setting the angular velociy as a constant, so if the circle gets bigger, the "distance" the sprite will travel on each tick will increase, and may lead to flickering.
这篇关于在 Cocos2d-iPhone 中围绕圆周移动精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!