[ios]等待方法完成目标 c 在开始下一种方法

发布时间: 2017/4/3 15:17:10
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在一个游戏与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:

奄奄一息的动画运行时,您添加完成事件那里发射意识

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

请注意,不只是复制和粘贴这,它未必能 100%目标 c 因为我目标 c 是生锈

此外我应该注意到,这将产生对自我,循环引用,所以你需要做自我疲软的版本。 我忘了怎么办,在目标 c 正确不过,我想说你可以添加类似 __weak SKNode *weakSelf = self; 去完成工作,但我记得告终在途中某地,所以不要用它所有的时间问题。

赞助商