0
votes

Okay so I have been trying to preposition a sprite node before adding it to the scene. The only problem is that I need to know the (0, 0.5) or (left, middle) position of the node, in scene coordinates before I can position it properly.

I know about the convertPoint:(CGPoint) toNode/fromNode:(SKSpriteNode *) methods and currently I have worked out the following within the Scene's code:

[node convertPoint:CGPointMake(0,0.5) toNode: self]

I also wasn't sure if it was confusing self (the scene) with self (the node), so I tried

SKScene *scene = self;
[node convertPoint:CGPointMake(0,0.5) toNode: scene]

I am pretty sure that I didn't have to make the distinction but I tried any ways.

The logged result of both attempts was (0,0.5).

The node.position is (50, 100).

In case the above is not clear, I am trying to find the position on the edge of the frame, which should be equal to the nodes width. The reason why I am not using width though is because I am placing it in respect to another node and the two nodes may be rotated.

The theories I am trying to reference are from Apples Spritekit Programming Guide

If there is an easier way to establish a distance between two nodes based on the width of one node, taking into account rotation, feel free to post it for I would love to know, although I still need the node conversion for other methods.

Thank you in advance for all of your help.

1
I'm not sure I understand what you want, but convertPoint:toNode: gets absolute position, not relative like 0,0.5. Check if CGRectGetMinX and CGRectGetMidY help in what you are looking for. - gzafra
I did find a way to use CGRectGetMinX, and CGRectGtMidY to do so, but it seemed very clunky. I know that when you set an anchor point you set it using (0.5,0.5) as the default centre and (0,0) for the bottom left. Knowing this I discovered that by setting the anchor point to (0,0.5) and then checking the nodes position would give me exactly what I need. The only problem is every time I set the anchor point to that position I have problems with the physics bodies. - Ryoku
Replying on the answer since I'm adding code. - gzafra

1 Answers

1
votes

You shouldn't change the anchor point once the node has been added as it will inherently change its position. If you are using anchor 0.5, 0.5 to rotate the nodes leave it like that. If you want to get the maxX point from a rotated node you could do something like this:

    SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithColor:[UIColor blackColor] size:CGSizeMake(50, 50)];
    float angle = - sprite.zRotation;
    CGPoint dirVect = CGPointMake(cosf(angle), sinf(angle));
    CGFloat distance = sprite.frame.size.width/2 + sprite2.frame.size.width/2;
    CGPoint destPoint = CGPointMake(sprite.position.x + (dirVect.x * distance),
                                    sprite.position.y + (dirVect.y * distance));


    sprite2.position = destPoint;
    [self addChild:sprite2];

Where sprite is the node you have rotated and sprite2 the node you want to add respect of the first node. distance should be the distance (excuse the pun) between the anchor points of the two nodes.

Let me know if this is what you are looking for. If not, a screenshot would help :)