等待方法完成以启动目标c中的下一个方法

时间:2016-02-09 18:10:42

标签: ios objective-c animation sprite-kit skscene

我正在使用Sprite套件制作游戏,基本上当"英雄"然后开始动画(在代码中称为" AnimBack")然后跳转到游戏场景,但现在当"英雄"它立即死于跳过场景游戏。这就是我现在所拥有的:

-(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];
 }

我做错了什么,我需要改变什么,所以它运行我在开始时的解释

1 个答案:

答案 0 :(得分:2)

当您运行节食动画时,您可以在那里添加完成事件以触发游戏结束

[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];
}];

注意,不要只是复制和粘贴它,因为我的目标c生锈,它可能无法在目标c中100%工作

另外我应该注意,这将创建一个循环引用自我,所以你将需要做一个弱版本的自我。我忘记了如何在目标c中正确地做到这一点,我想说你可以添加类似__weak SKNode *weakSelf = self;的东西来完成工作,但我记得我在途中遇到了问题,所以不要全部使用它时间。