First post,
I'm in Apple's SpriteKit framework...(new programmer) and I have a bunch of nodes that are stationary. How do I select/grab a particular node if I know the coordinates (CGPoints)? I basically want to say, grab that node at (x,y) and do something with it (i.e. change color)
Thanks
If you know the CGPoint, you use the command nodeAtPoint: However, this command only returns the deepest descendant that intersects a point. If you have the possibility of having more than one node at your CGPoint, you should use the command nodesAtPoint: instead.
Read up on the details in the SKNode Class Reference - section titled Determining If a Point Lies in a Node.
** Update **
CGPoint myPoint = CGPointMake(10, 10);
SKNode *myNode = [self nodeAtPoint:myPoint];
Where self either is or needs to be replaced with the parent node of the node you are looking for.
Related
I am currently working at a 2D endless runner in SpriteKit. I create new Level sequences using the Scene Editor and then add them as a SKReferenceNode to the main scene.
The problem is, that I need to get the position of the single nodes inside of the SKReferenceNode, but the ones I get, are relative to the SKReferenceNodes coordinate system and not the main one.
How can I get the position of the nodes relative to the main scene?
you'll want to use the convert func
func convert(_ point: CGPoint, to node: SKNode) -> CGPoint
Description
Converts a point in this node’s coordinate system to the coordinate system of another node in the node tree.
Parameters
point - A point in this node’s coordinate system.
node - Another node in the same node tree as this node.
Returns
The same point converted to the other node’s coordinate system.
I have following hierarchy:
Scene -> Root node -> NodeA -> NodeB (anchor node that's rotated) -> NodeC
how can I figure out visual position of NodeC in Root node coordinate space?
I've tried several variants with convertPoint:toNode: and convertPoint:fromNode: but numbers i'm getting don't match.
I've also looked at Get visible location of SpriteKit node after parent rotation, which seems to be close to my issue, but no luck there either.
Found what I was missing - convertToPoint needs to be called from the node in which final CGPoint value will be represented in. So, this works:
// point is CGPointZero because it is center of NodeC
let point = rootNode.convertPoint(CGPointZero, fromNode: NodeC) as CGPoint!
I have a snake clone, and I use few coordinates system in that. One is coordinates in map node, another is world node, where everything that scrolls resides and one is scene - where my snake head resides.
The problem is with converting points from one system to another. It can become very tedious to nest from one system to another and this also leads me to having to calculate all positions in scene class.
Take for example the code:
CGPoint tempPoint = [self convertPoint:[self convertPoint:[self.map nextWaypointForEnemyOnPosition:[self convertPoint:[self convertPoint:enemy.position fromNode:self.worldNode] toNode:self.map] andHeading:enemy.heading] fromNode:self.map] toNode:self.worldNode];
This gets out of hand very fast. How do I fix this and ease the point conversions?
Keep all nodes that need to have coordinates compared on one layer (node) - this greatly simplifies things.
i have managed to create a SKspritenode , Make it move around , although i want to change the color of every CGPOINT that the node Passes by .
Anyway to do this ?
Use CGShapeNode to draw a path which consists of CGPoints your node passed throught.
For direction on creating a path from points check out answer to this question
I have a node and I've fixed the position of that node. Then I added a sprite as a child of that node (which I've added earlier). After that I try to rotate the node by some angle.
As a result sprite is also rotating and its center point is position of node. Now my question is, how can I get the position of sprite?
Sprite position is the same - "Center of node".
I don't understand what exact position do you want to evaluate. And I hope follow methods will help you:
Each CCNode (and all sublcasses CCSprite, CCLayer etc) have follow helpers that very useful for evaluation coords between nodes:
- (CGPoint)convertToNodeSpace:(CGPoint)worldPoint;
- (CGPoint)convertToWorldSpace:(CGPoint)nodePoint;
- (CGPoint)convertToNodeSpaceAR:(CGPoint)worldPoint;
- (CGPoint)convertToWorldSpaceAR:(CGPoint)nodePoint;