在Cocos2d-iPhone中围绕圆周移动精灵 [英] Moving a sprite around the circumference of a circle in Cocos2d-iPhone

查看:147
本文介绍了在Cocos2d-iPhone中围绕圆周移动精灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cocos2d的新手。我已经看了一下文档看起来很简单,比起你需要做的使用基本的Iphone类,如UIAnimation等。



我想移动一个精灵(例如,鸟,飞机或汽车)围绕其圆周的圆的中心,平滑地,使得即使子画面也相应地旋转。



在Cocos2d中如何实现?



感谢。

解决方案



为了做到这一点,你可以选择一个选择器来定期运行选择器,当它被调用时,您可以查看 CGPointExtension.m 的函数。特别是,您可以使用 ccpForAngle ccpMult ccpAdd 。你可以这样做:

  //假设类是CCNode的子类,并且具有以下属性:
// radius:你想让精灵移动的圆的半径。
// circleCenter:圆的中心
// currentAngle:当前sprite的角度
- (void)tick {
float anglePerTick = 0.1; //这将决定你的速度
currentAngle + = anglePerTick;
self.position = ccpAdd(ccpMult(ccpForAngle(currentAngle),radius)),
circleCenter);
self.rotation = currentAngle * 180 / M_PI; //从弧度转换为度
}

这种方法的主要问题是将角速度设置为常数,所以如果圆变大,子画面在每个节拍上移动的距离将增加,并且可能导致闪烁。


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.

How is this possible in Cocos2d? It would be very helpful if someone can post some basic code.

Thanks.

解决方案

One way to do it is to schedule a selector to run periodically, and when it's called, move your sprite over the circle.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆