简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM