Question

I am implementing a game which loads background graphics dynamically, depending on currently visible area. The background is drawn with using SKShapeNode. In order to save memory, I am drawing only a small part of the CGPath. The problem is that no matter what CGPath coordinates I use, the resulting frame of SKShapeNode will always contain the (0, 0) point. Here is an example:

SKShapeNode *shapeNodePos = [SKShapeNode node];
CGMutablePathRef pathPos = CGPathCreateMutable();
CGPathAddRect(pathPos, NULL, CGRectMake(50, 50, 50, 50));
shapeNodePos.path = pathPos;
CGPathRelease(pathPos);
NSLog(@"Frame pos = (%f, %f, %f, %f)", shapeNodePos.frame.origin.x, shapeNodePos.frame.origin.y, shapeNodePos.frame.size.width, shapeNodePos.frame.size.height);

The result is:

Frame pos = (0.000000, 0.000000, 100.000000, 100.000000)

However, I would expect the SKShapeNode.frame to be limited only to CGPath contents:

Frame pos = (50.000000, 50.000000, 50.000000, 50.000000)

Is it a bug in SpriteKit or actually an expected behaviour? If that small CGPath rectangle is drawn considerably far from the (0, 0) point can it also negatively impact the performance or will Sprite Kit ignore empty area of the frame?

Thanks in advance!

Was it helpful?

Solution

From my tests, it appears that unless you specifically set the node's position before creating your CGMutablePathRef, the starting point will always be (0,0). However, setting the node's position beforehand gives you the results you expected.

SKShapeNode *shapeNodePos = [SKShapeNode node];
shapeNodePos.position = CGPointMake(50, 50); // <<< Set node's position
CGMutablePathRef pathPos = CGPathCreateMutable();
CGPathAddRect(pathPos, NULL, CGRectMake(0, 0, 50, 50));
shapeNodePos.path = pathPos;
CGPathRelease(pathPos);

The NSLog now prints

Frame pos = (50.000000, 50.000000, 50.000000, 50.000000)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top