在cocos2d中获取CCSprite的绝对位置 [英] Getting absolute position of CCSprite in cocos2d

查看:243
本文介绍了在cocos2d中获取CCSprite的绝对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏中,我有一个 CCSprite 绕另一个 CCSprite 旋转,就像电子绕原子核旋转一样。我将电子作为原子核的子代,以使动画更简单。我所要做的就是旋转原子核,并且电子非常顺畅地跟随套件。

In my game, I have a CCSprite that orbits another CCSprite, much like an electron orbiting a nucleus. I have the electron as a child of the nucleus, to make animation much simpler. All I have to do is rotate the nucleus and the electron follows suite quite nicely.

但是,我的问题出在想让轨道动画显得更时髦一点,要么添加诸如粒子系统轨迹之类的东西,要么沿着电子路径添加带状效应。我不能简单地将粒子系统添加到电子本身,因为粒子也不能正确地跟随,因为它们也会被原子核旋转。如果我将粒子系统添加到 self 中,则它们将正确显示,但与应该跟踪的对象不在同一位置。

However, my problem comes from wanting to have the orbit animation appear a little snazzier, by either adding something like a particle system trail, or a ribbon effect following the path of the electron. I cannot simply add a particle system to the electron itself, because the particles don't follow correctly, since they are being rotated by the nucleus as well. If I add the particle system to self, then they appear correctly, but not in the same position as the object they are supposed to be trailing.

我的问题是:

有没有一种方法可以获取物体(例如电子)的场景位置,而不是仅仅具有

Is there a way to get the scene position of an object, say the electron, as opposed to only having access to it's position relative to it's parent?

谢谢。

推荐答案

是的!

每个CCNode及其后代都有能力获得相对于场景的位置:

Each CCNode and its descendants has the ability to get a position relative to the scene:

CGPoint worldCoord = [mySprite convertToWorldSpace:mySprite.position];

CGPoint worldCoord = [mySprite convertToWorldSpace: mySprite.position];

此worldCoordinate将相对于场景而不是父节点!

This worldCoordinate will be relative to the scene as opposed to the parent node!

希望有帮助! ^ _ ^

Hope this helped! ^_^

这篇关于在cocos2d中获取CCSprite的绝对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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