Ios 精灵套件:获取绝对节点位置

Ios 精灵套件:获取绝对节点位置,ios,sprite-kit,Ios,Sprite Kit,在本例中,如何获取_childNode的绝对位置: 我有一个父母: _世界节点 我有一个孩子: _子节点 我将_childNode位置设置为10,10 我把世界节点旋转90度 现在,当我查询_childNode位置时,我得到了10,10。当然,情况已不再如此。甚至做[\u childNode calculateacumeratedframe]都会得到10,10的CGRect 你知道如何返回正确的位置(-10,10)吗? 这是我测试中的代码(刚刚放入init,所有日志返回10,10) 多谢各位,

在本例中,如何获取_childNode的绝对位置:

我有一个父母: _世界节点

我有一个孩子: _子节点

我将_childNode位置设置为10,10

我把世界节点旋转90度

现在,当我查询_childNode位置时,我得到了10,10。当然,情况已不再如此。甚至做[\u childNode calculateacumeratedframe]都会得到10,10的CGRect

你知道如何返回正确的位置(-10,10)吗? 这是我测试中的代码(刚刚放入init,所有日志返回10,10)

多谢各位,
简而言之:旋转不会改变节点的位置

SKSpriteNode
position
返回该节点的
锚点的位置


旋转
SKSpriteNode
时,它将在其
锚点上旋转,因此节点的
锚点
将保持与旋转前相同的位置。这就是为什么你总是得到(10,10)

你的代码中有很多错误

1-

_worldNode.position = screenCentre;  //This places the node at the SKScene centre
_worldNode.zRotation = 90;
应该是

_worldNode.position = CGPointZero; //This places the node at the origin.
场景的默认坐标系的原点位于左下角

2-

_worldNode.position = screenCentre;  //This places the node at the SKScene centre
_worldNode.zRotation = 90;
应该是

_worldNode.zRotation = M_PI_2;
zRotation是以弧度而不是度来测量的

3-

_worldNode.position = screenCentre;  //This places the node at the SKScene centre
_worldNode.zRotation = 90;
查看代码中的两个NSLog:

NSLog(@"position of child after rotation %f, %f", _childNode.position.x, _childNode.position.y); // - 1

CGPoint position = _childNode.position;
[_childNode convertPoint:position toNode:_worldNode]; //returns a CGPoint, where are you storing it?

NSLog(@"position of child after conversion %f, %f", position.x, position.y); // - 2
位置
变量实际上没有转换。该方法返回另一个CGPoint,该CGPoint需要依次记录

但是,这将返回错误的位置,因为
\u childNode。position
存储它相对于其父节点(即
\u worldNode
)的位置,因此使用
convertPoint
to
\u worldNode
将\u childNode上的(10,10)转换为\u worldNode,使其成为(20,20)

请尝试使用以下行:

CGPoint position = [self convertPoint:_childNode.position fromNode:_worldNode];
NSLog (@"Position: %@", [NSValue valueWithCGPoint:position]);

这将为您提供场景中的位置,这正是您的目标。

非常感谢!1.事实上,这对我现在所做的很好,尽管我很感激它混淆了我给出的例子,对此我很抱歉。2.又一次,快速的例子,愚蠢的错误,忘记了它需要rads。3.这个解决方案是可行的,这是一个愚蠢的错误,我假设点的位置是隐式转换的(?)。