Moving a sprite around the circumference of a circle in Cocos2d-iPhone(在 Cocos2d-iPhone 中围绕圆周移动精灵)
问题描述
我是 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 中围绕圆周移动精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Cocos2d-iPhone 中围绕圆周移动精灵
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01