Reputation: 235
i'm making a game with Sprite kit, basically when "hero" dies, then starts animation(in code called "AnimBack") and after that jumps to game over scene, but now when "hero" dies it right away jumps to game over scene. Here what I have for now :
-(void)AnimBack{
Animation =[SKSpriteNode spriteNodeWithImageNamed:@"die0"];
Animation.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
SKTextureAtlas *atlas =[SKTextureAtlas atlasNamed:@"die"];
SKTexture *die0 = [atlas textureNamed:@"die0"];
SKTexture *die1 = [atlas textureNamed:@"die1"];
SKTexture *die2 = [atlas textureNamed:@"die2"];
SKTexture *die3 = [atlas textureNamed:@"die3"];
NSArray *backatlas = @[die0, die1, die2, die3];
SKAction *backatlasanimation = [SKAction animateWithTextures:backatlas timePerFrame:0.1];
SKAction *wait = [SKAction waitForDuration:0.1];
SKAction *backaction = [SKAction sequence:@[wait, backatlasanimation]];
[Animation runAction:backaction];
[self addChild:Animation];
}
- (void)didBeginContact:(SKPhysicsContact*)contact {
//-----------------------------------//
if ((firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == bottomCategory) || (firstBody.categoryBitMask == secballCategory && secondBody.categoryBitMask == bottomCategory)) {
[self AnimBack];
GameOver* gameOver = [[GameOver alloc] initWithSize:self.frame.size playerLose:YES];
SKTransition *transition = [SKTransition pushWithDirection:SKTransitionDirectionUp duration:1.0f];
[self.view presentScene:gameOver transition:transition];
}
What Iam doing wrong and what i need to change so it runs how i explained at beginning
Upvotes: 0
Views: 107
Reputation: 16827
when you run the dieing animation, you add the completion event there to fire off gameover
[hero runAction:dieingAnimationAction completion:
{
GameOver* gameOver = [[GameOver alloc] initWithSize:self.frame.size playerLose:YES];
SKTransition *transition = [SKTransition pushWithDirection:SKTransitionDirectionUp duration:1.0f];
[self.view presentScene:gameOver transition:transition];
}];
Note, do not just copy and paste this, it may not work 100% in objective c since my objective c is rusty
also I should note that this will create a circular reference to self, so you are going to need to do a weak version of self. I forget how to do that in objective c correctly though, I wanna say you can add something like __weak SKNode *weakSelf = self;
to get the job done, but I remember I ended up with problems somewhere along the way, so do not use it all the time.
Upvotes: 2