xcode 在cocos2d中获取CCSprite的绝对位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10215869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:00:59  来源:igfitidea点击:

Getting absolute position of CCSprite in cocos2d

objective-ciosxcodecocos2d-iphoneccsprite

提问by GreenWire

In my game, I have a CCSpritethat 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.

在我的游戏中,我有一个CCSprite绕另一个 运行CCSprite,就像电子绕着原子核运行一样。我有电子作为原子核的孩子,使动画更简单。我所要做的就是旋转原子核,电子就会很好地跟随。

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.

然而,我的问题来自于想让轨道动画看起来更时髦,可以通过添加粒子系统轨迹之类的东西,或者沿着电子路径添加丝带效果。我不能简单地向电子本身添加一个粒子系统,因为粒子不能正确跟随,因为它们也被原子核旋转。如果我将粒子系统添加到self,则它们会正确显示,但与它们应该尾随的对象不在同一位置。

My question is this:

我的问题是这样的:

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?

有没有办法获得一个物体的场景位置,比如电子,而不是只能访问它相对于它的父级的位置?

Thanks.

谢谢。

回答by wayway

Yes there is!

就在这里!

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

每个 CCNode 及其后代都可以获取相对于场景的位置:

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

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

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

这个世界坐标将相对于场景而不是父节点!

Hope this helped! ^_^

希望这有帮助!^_^

回答by AnaZ

A later edit:

后来的编辑:

When you do:

当你这样做时:

[aSprite convertToWorldSpace:position];

You are actually getting the global coordinates of position in aSprite's coordinate system. If you want to translate aSprite's position to global space you need to ask for it's parent to do the translation for you, because the sprite.position is already in it's parents coordinates system.

您实际上是在 aSprite 的坐标系中获取位置的全局坐标。如果您想将 aSprite 的位置转换到全局空间,您需要请求它的父级为您进行转换,因为 sprite.position 已经在它的父级坐标系统中。

Hope it explains it

希望它解释它



原答案:

For me this solution did not work

对我来说,这个解决方案不起作用

I had a 2 level hierarchy where mySprite is a child of a CCSprite that is a child of the scene.

我有一个 2 级层次结构,其中 mySprite 是 CCSprite 的子级,CCSprite 是场景的子级。

Bottom line: This code fixed the problem for me:

底线:此代码为我解决了问题:

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

This is the hierarchy structure that required my solution: myScene -> mySpriteParent -> mySprite

这是需要我的解决方案的层次结构:myScene -> mySpriteParent -> mySprite

mySprite.position:29,254
mySpriteParent.position:533,57

Solution1 - wrong result:

解决方案 1 - 错误的结果:

[mySprite convertToWorldSpace: mySprite.position]:91,418

Solution2 - right result:

解决方案 2 - 正确的结果:

[[boxSprite parent] convertToWorldSpace:boxSprite.position]:253.5,275.5

Perhaps this solution will help someone And maybe someone will explain why this solution works and not the other

也许这个解决方案会帮助某人也许有人会解释为什么这个解决方案有效而不是另一个