Get visible location of SpriteKit node after parent rotation

▼魔方 西西 提交于 2019-12-24 03:12:24

问题


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 |
+-----+-----+-----+

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' 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.


回答1:


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];

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

  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.


来源:https://stackoverflow.com/questions/25224319/get-visible-location-of-spritekit-node-after-parent-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!