Lucas
Lucas

Reputation: 713

Sprite Kit Scene not presenting

I have a sprite-kit game where I need to be checking often to see if the player has lost

- (void)update:(NSTimeInterval)currentTime
{
 for (SKSpriteNode *sprite in self.alienArray ) {
    if (sprite.position.y < 10) {
        LostScene *lostScene = [[LostScene alloc] initWithSize: CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        NSLog(@"about to present");
        [self.view presentScene:lostScene transition:[SKTransition fadeWithDuration:0.5]];
    }

}

}

but when this method gets called (which I know is happening), no scene presents. What am I doing wrong? I believe it has something to do with the transition, because when I take it out, it works fine

Upvotes: 0

Views: 1234

Answers (2)

BSevo
BSevo

Reputation: 793

You should add a property of the existing scene like: BOOL playerHasLost and edit your update method:

- (void)update:(NSTimeInterval)currentTime
{
if(!playerHasLost)
{
for (SKSpriteNode *sprite in self.alienArray ) 
{
  if (sprite.position.y < 10)
  {
    playerHasLost = YES;
    LostScene *lostScene = [[LostScene alloc] initWithSize: CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    NSLog(@"about to present");
    [self.view presentScene:lostScene transition:[SKTransition fadeWithDuration:0.5]];
    break;//to get out of for loop
  }
}
}
}

So this way as soon as the sprite get to position that you treat as lost position it will set the variable to YES, present the scene and it will not do it again.

Upvotes: 5

CodeSmile
CodeSmile

Reputation: 64478

The reason is simply that the update method gets called every frame, with a running transition the scene will be presented anew every frame and thus it appears as if nithing is happening. You should see the NSLog spamming the log console.

Upvotes: 0

Related Questions