我有两个 Sprite 节点英雄和敌人,都应用了矩形物理主体。 在更新中,当英雄到达某个点时,例如hero.position.y <= 300,我希望敌人在英雄向下移动时旋转并面对英雄。
我找到的唯一示例代码是 Adventure from Apple它有一个faceTo类,但我发现它使用起来非常复杂。我正在寻找一个好的、干净的解决方案。
提前致谢。
最佳答案
最基本的实现应该如下所示:
- (void)rotateNode:(SKNode *)nodeA toFaceNode:(SKNode *)nodeB {
CGFloat angle = atan2f(nodeB.position.y - nodeA.position.y, nodeB.position.x - nodeA.position.x);
if (nodeA.zRotation < 0) {
nodeA.zRotation = nodeA.zRotation + M_PI * 2;
}
[nodeA runAction:[SKAction rotateToAngle:angle duration:0]];
}
了解正在发生的事情非常重要。查找并阅读有关 atan2 ( http://en.wikipedia.org/wiki/Atan2 ) 的信息,并尝试了解上面的代码是如何工作的。
关于objective-c - Sprite 套件 - 面对英雄? (向另一个节点的方向旋转),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23535777/