Search code examples
objective-csprite-kitskshapenode

SKShapeNode.Get Radius?


Is it possible to get an SKShapeNode's radius value?

The problem is, I need to create an identical circle after the user releases it, whilst removing the first circle from the view.

    SKShapeNode *reticle = [SKShapeNode shapeNodeWithCircleOfRadius:60];
    reticle.fillColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    reticle.strokeColor = [UIColor clearColor];
    reticle.position = CGPointMake(location.x - 50, location.y + 50);
    reticle.name = @"reticle";
    reticle.userInteractionEnabled = YES;
    [self addChild:reticle];

    [reticle runAction:[SKAction scaleTo:0.7 duration:3] completion:^{
        [reticle runAction:[SKAction scaleTo:0.1 duration:1]];
    }];

Then

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
....
    SKNode *node = [self childNodeWithName:@"reticle"];

    //take the position of the node, draw an imprint

    /////////Here is where I need to get the circle radius.

    [node removeFromParent];

}

How does one take the circle radius, so I can then say

SKShapeNode *imprint = [SKShapeNode shapeNodeWithCircleOfRadius:unknownValue];

I've tried, making an exact copy of the circle with

SKShapeNode *imprint = (SKShapeNode *)node;

However, this still follows the animation where as I need it to stop, at the point it was at. I don't want to take a "stopAllAnimations" approach.


Solution

  • You can use the shape node's path property to determine the bounding box of the shape with CGPathGetBoundingBox(path). From the bounds, you can compute the radius of the circle by dividing the width (or height) by 2. For example,

    CGRect boundingBox = CGPathGetBoundingBox(circle.path);
    CGFloat radius = boundingBox.size.width / 2.0;