父旋转后获取 SpriteKit 节点的可见位置 [英] Get visible location of SpriteKit node after parent rotation

查看:56
本文介绍了父旋转后获取 SpriteKit 节点的可见位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Sprite Kit(呃,也许学习"更合适).我有一个方形游戏板:

I am using Sprite Kit (uh, perhaps "learning" is more apropos). I have a square game board:

+-----+-----+-----+
| 0,2 | 1,2 | 2,2 |
+-----+-----+-----+
| 0,1 | 1,1 | 2,1 |
+-----+-----+-----+
| 0,0 | 1,0 | 2,0 |
+-----+-----+-----+

在测试期间,我将精灵添加到板位置 2,0.然后我将板逆时针旋转 90 度,使精灵 - 视觉上 - 在位置 2,2.当然,由于精灵的位置不会因旋转而改变,因此实际的 x,y 坐标不会改变.我的算法等都是围绕视觉"板位置而设计的,让我无需为所有 4 个旋转角度编写代码.所以,我在内部知道 2,0 处的精灵移动到 2,2(视觉上);我现在需要使用在棋盘位置 2,2 处看到的精灵".如何获得精灵的视觉"x,y 坐标?我试过了:

During testing I am adding a sprite to board position 2,0. I then rotate the board 90 degrees counter-clockwise leaving the sprite - visually - in position 2,2. Of course, since the position of sprites aren't modified by rotation the actual x,y coords don't change. My algorithms, etc. are geared around 'visual' board positions saving me from needing to write code for all 4 rotation angles. So, I know internally that the sprite at 2,0 moved to 2,2 (visually); and I now need to work with "the sprite seen at board position 2,2". How do I get hold of the 'visual' x,y coords of the sprite? I've tried:

[_board convertPoint:p toNode:self];
[_board convertPoint:p toNode:self.view];     // didn't expect this to work

'p' 是我从棋盘位置 2,2 计算的 CGPoint.这不是代码问题 - 它更像是一个菜鸟我该怎么做"的问题.

'p' is a CGPoint that I've calculated from board position 2,2. This isn't a code question - it's more of a noobie "how do I do this" question.

推荐答案

要获得 SKNode视觉位置,您需要转换它的 .position 在其 .parent 的坐标系中到其 .scene 的坐标系:

To get an SKNode's visual position, you'd want to convert its .position in its .parent's coordinate system to its .scene's coordinate system:

CGPoint vPos = [node.parent convertPoint:node.position toNode:node.scene];

看起来您的第一次尝试 ([_board convertPoint:p toNode:self]) 应该可以工作,只要:

It looks like the first attempt you made ([_board convertPoint:p toNode:self]) should work, as long as:

  1. p 是您在 _board 坐标系中查找的节点内的有效点.
  2. 从您的 SKScene 调用该代码,以便 self 引用节点所在的场景.
  1. p is a valid point inside the node you're looking for in _board's coordinate system.
  2. That code is called from your SKScene so that self references the scene the node is in.

这篇关于父旋转后获取 SpriteKit 节点的可见位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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