Liam Stockhomme
Liam Stockhomme

Reputation: 523

Why does this image keep reappearing? Objective-C

I've set up an texture so that it spawns above the scene, and slowly scrolls down the screen for scenery. The scrolling and appearing of the texture are fine. They work how I want. The issue is that it reappears after it's left the scene even though I've set it up to be removed from the parent. This is the code:

Spawning of the item:

-(void)initalizingScrollingBackground
{
    for (int i = 0; i < 2; i++) {

        SKSpriteNode *tumbleWeed = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"tumbleWeed.png"] size:CGSizeMake(40, 40)];
        tumbleWeed.position = CGPointMake(160, -10);
        tumbleWeed.zPosition = 1.0;
        tumbleWeed.name = @"tumbleWeed";
        [bg addChild:tumbleWeed];

    }

} 

Moving the item, and deleting it when it is off screen:

    -(void)moveItems {

    [self enumerateChildNodesWithName:@"tumbleWeed" usingBlock:^(SKNode *node, BOOL *stop) {
        SKSpriteNode * tumbleWeed = (SKSpriteNode *)node;
        CGPoint bgVelocity = CGPointMake(0, -BG_VELOCITY);
        CGPoint amtToMove = CGPointMultiplyScalar(bgVelocity,_dt);
        tumbleWeed = CGPointAdd(amtToMove, tumbleWeed.position);

        if (tumbleWeed.position.y > self.size.height + 10) {
            [tumbleWeed removeFromParent];
        }

    }];

}

I don't understand why the item reappears at the top of the scene and restarts the scrolling? Any reasons why? How can I fix it?

Thanks.

Upvotes: 1

Views: 86

Answers (1)

Ian
Ian

Reputation: 3836

Two ideas for you:

1) Put a breakpoint in -(void)initalizingScrollingBackground so that the app will pause there. Then look at the call stack to see how the call to this method is being triggered.

2) Instead of calling removeFromParent on the child object, try starting that action from the parent. Ex: [bg removeChild: tumbleWeed]; I've seen occasions in other projects where making this call from the child can be problematic, and so I've adopted the practice of always having the add/remove action start with the parent.

Good luck! Hope this helps....

Upvotes: 2

Related Questions