等待方法完成以在 objective c 中启动下一个方法
Wait for method complete to start next method in objective c
我正在用 Sprite 套件制作游戏,基本上当 "hero" 死亡时,然后开始动画(在代码中称为 "AnimBack"),然后跳转到游戏结束场景,但现在当 "hero" dies it right away jumps to game over scene。这是我现在拥有的:
-(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];
}
我做错了什么以及我需要更改什么才能按照我一开始解释的那样运行
当你 运行 死亡动画时,你在那里添加完成事件以触发游戏结束
[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];
}];
注意,不要只是复制和粘贴它,它在 objective c 中可能无法 100% 工作,因为我的 objective c 已经生锈了
我还应该注意,这将创建对 self 的循环引用,因此您将需要做一个 self 的弱版本。我忘记了如何在 objective c 中正确地做到这一点,我想说你可以添加类似 __weak SKNode *weakSelf = self;
的东西来完成工作,但我记得我在这个过程中的某个地方遇到了问题,所以不要一直用它。
我正在用 Sprite 套件制作游戏,基本上当 "hero" 死亡时,然后开始动画(在代码中称为 "AnimBack"),然后跳转到游戏结束场景,但现在当 "hero" dies it right away jumps to game over scene。这是我现在拥有的:
-(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];
}
我做错了什么以及我需要更改什么才能按照我一开始解释的那样运行
当你 运行 死亡动画时,你在那里添加完成事件以触发游戏结束
[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];
}];
注意,不要只是复制和粘贴它,它在 objective c 中可能无法 100% 工作,因为我的 objective c 已经生锈了
我还应该注意,这将创建对 self 的循环引用,因此您将需要做一个 self 的弱版本。我忘记了如何在 objective c 中正确地做到这一点,我想说你可以添加类似 __weak SKNode *weakSelf = self;
的东西来完成工作,但我记得我在这个过程中的某个地方遇到了问题,所以不要一直用它。