8
votes

I'm new to iOS's Sprite Kit. I want to add a shape node to my scene. The scene is gray, and the shape is a white circle in the center of the scene. My scene code is below. For some reason the last line that adds the node to the scene causes the node count to go up by two. If I leave that line out, there's 0 nodes and just a gray scene. But if I leave the line then the circle is there but the node count is 2. This is a big problem because as I add more nodes to the circle the node count is double what it should be and slows things down. Anyone know what the problem is? Much appreciated!

@interface ColorWheelScene()
@property BOOL contentCreated;
@end

@implementation ColorWheelScene

- (void)didMoveToView:(SKView *)view {
    if(!self.contentCreated) {
        [self createSceneContents];
        self.contentCreated = YES;
    }
}

- (void)createSceneContents {
    self.backgroundColor = [SKColor grayColor];
    self.scaleMode = SKSceneScaleModeAspectFit;

    SKShapeNode *wheel = [[SKShapeNode alloc]init];
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(0.0, 0.0)];
    [path addArcWithCenter:CGPointMake(0.0, 0.0) radius:50.0 startAngle:0.0 endAngle:(M_PI*2.0) clockwise:YES];
    wheel.path = path.CGPath;
    wheel.fillColor = [SKColor whiteColor];
    wheel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    [self addChild:wheel];
}

@end
1
have you verified that create scene contents is truly ever being called once? maybe put a log in or break pointAwDogsGo2Heaven
Yes. I put a log there and it's only called once.Hash88
So if I understand you correctly, if you add this shape once, it says their are 'two' nodes, if you add it twice it would say their are 'four' nodes.?AwDogsGo2Heaven
Yeah. I just did that - added two shapes, and it was 4 nodes.Hash88

1 Answers

15
votes

You get +1 node for adding the fill to the circle

So,

- (void) makeACircle
{
    SKShapeNode *ball;
    ball = [[SKShapeNode alloc] init];

// stroke only = 1 node
//    CGMutablePathRef myPath = CGPathCreateMutable();
//    CGPathAddArc(myPath, NULL, 0,0, 60, 0, M_PI*2, YES);
//    ball.path = myPath;
//    ball.position = CGPointMake(200, 200);
//    [self addChild:ball];

// stroke and fill = 2 nodes
    CGMutablePathRef myPath = CGPathCreateMutable();
    CGPathAddArc(myPath, NULL, 0,0, 60, 0, M_PI*2, YES);
    ball.path = myPath;
    ball.fillColor = [SKColor blueColor];
    ball.position = CGPointMake(200, 200);
    [self addChild:ball];

}