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

2024-08-12移动开发问题
1

本文介绍了在 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 中的函数.特别是,您可以使用 ccpForAngleccpMultccpAdd.你可以这样做:

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 中围绕圆周移动精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21