0
votes

I'm currently trying SpriteKit for first time and using some actions. I was under the impression that every node/sprite you create you would need to remove it but for some reason they are removed by themselfs as soon as they pass the screen height. Is this a normal behavior in SpriteKit?

Here is the code. I'm basically using an action to animate a sprite across the screen (from bottom to top in protrait-mode), the funny thing is that the node count stays at 0 no matter how many sprites I create, it actually counts them but it subtracts them as soon as they pass the screen height.

-(void) shoot
{
    bullet = [SKSpriteNode spriteNodeWithImageNamed:@"bullet"];
    bullet.position =  CGPointMake(airPlane.position.x, airPlane.position.y + 50);
    [self addChild:bullet];

    SKAction *moveBullet = [SKAction moveTo:CGPointMake(airPlane.position.x, self.size.height + 20) duration:0.5];

    [bullet runAction:moveBullet];
}

If I change my action to where the sprite doesn't cross the screen height then the node count keeps the count.

Change this line...

SKAction *moveBullet = [SKAction moveTo:CGPointMake(airPlane.position.x, self.size.height + 20) duration:0.5];

... to this

SKAction *moveBullet = [SKAction moveTo:CGPointMake(airPlane.position.x, self.size.height) duration:0.5];

Again Is this a normal behavior in SpriteKit?

Thanks a lot

2

2 Answers

1
votes

The node count displayed on the lower right corner is just an indication of the nodes which are being rendered on the screen. They are not an indication of the total nodes in the scene.

Use the following code

[skView setValue:@(YES) forKey:@"_showsCulledNodesInNodeCount"];

At the same place where you call

skView.showsFPS = YES;
skView.showsNodeCount = YES;

Also, please have a look at this answer.

1
votes

that can be a memory leak because sprite kit holds textures in the memory for further use for better result remove it from the screen when its passing out of the screen and set it manually to nil

for eg

 [self enumerateChildNodesWithName:name usingBlock:^(SKNode *node, BOOL *stop) {

            if(node.x>900)
            {
                //remove all the action
               [node removeAllActions];
                //remove node from parent
               [node removeFromParent];
                //set nil//not required set automatically to nil when node is remove from parent 
               node=nil;
             }
        }];