问题
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:
p
is a valid point inside the node you're looking for in_board
's coordinate system.- That code is called from your
SKScene
so thatself
references the scene the node is in.
来源:https://stackoverflow.com/questions/25224319/get-visible-location-of-spritekit-node-after-parent-rotation