如何用另一个替换SpriteNode

时间:2014-02-19 21:31:51

标签: ios ios7 sprite-kit skspritenode sktexture

我在做什么:

我通过重复计时器每隔几秒钟设置一次炸弹,不断调用以下方法。

//in my init method

        SKTexture *Bomb5 = [SKTexture textureWithImageNamed:@"Bomb5.gif"];

        SKTexture *Bomb4 = [SKTexture textureWithImageNamed:@"Bomb4.gif"];

        SKTexture *Bomb3 = [SKTexture textureWithImageNamed:@"Bomb3.gif"];

        SKTexture *Bomb2 = [SKTexture textureWithImageNamed:@"Bomb2.gif"];

        SKTexture *Bomb1 = [SKTexture textureWithImageNamed:@"Bomb1.gif"];

        //SKTexture *explode = [SKTexture textureWithImageNamed:@"explosionnn.gif"];

        countdown = [SKAction animateWithTextures:@[Bomb5,Bomb4, Bomb3, Bomb2, Bomb1] timePerFrame:1];

-(void)setBomb{

    SKSpriteNode *bombb;
    SKSpriteNode *explostionn;

    bombb = [SKSpriteNode spriteNodeWithImageNamed:@"Bomb5.gif"];
    explosionn = [SKSpriteNode spriteNodeWithImageNamed:@"explosionnn.gif"];

    //set bomb in random locations on screen
    NSLog(@"Placing Bomb");
    int randomYAxix = [self getRandomNumberBetween:0 to:screenRect.size.height];
    int randomXAxix = [self getRandomNumberBetween:0 to:screenRect.size.width];

    bombb.position = CGPointMake(randomYAxix, randomXAxix);
    self.explosion.position = CGPointMake(randomYAxix, randomXAxix);
    bombb.size = CGSizeMake(35, 35);
    explosionn.size = CGSizeMake(90, 90);

    SKAction *removeNode = [SKAction removeFromParent];
    //SKAction *changeHeight = [SKAction resizeToHeight:90 duration:0];
    //SKAction *changeWidth = [SKAction resizeToWidth:90 duration:0];

    SKAction *sequence = [SKAction sequence:@[countdown, removeNode]];
    [bombb runAction: sequence];
    //add code here that adds the explosion sprite once the sequence has complete
    [self addChild:bombb];


}

我想要的是什么:

在动画结束后,下一个精灵(爆炸精灵)出现在炸弹所在的位置。

问题:

我没有采取行动取代精灵或帮助我正在尝试做的事情。 SKAction Class Reference 但我确实看到了这个任务customActionWithDuration:actionBlock:但不知道如何使用它,也无法通过搜索找到任何例子。

问题:

有人可以告诉我如何使用创建此任务,在5秒炸弹精灵动画结束后用我的爆炸精灵替换我的炸弹精灵吗?谢谢

2 个答案:

答案 0 :(得分:0)

有一个SKAction将精灵的纹理设置为另一个 - 所以你甚至不必删除序列中的炸弹节点,只需替换纹理。您可以在完成块中使用它,一旦您的操作完成就会执行。

[bombb runAction:sequence completion:^{
    [bombb runAction:[SKAction setTexture:explosionn.texture]];
}];

或者,如果你想把整个精灵放在那里:

explosionn.position = bomb.position;
[bombb runAction:sequence completion:^{
    [self addChild:explosionn];
}];

答案 1 :(得分:0)

你可以试试这个:

[self runAction:sequence
     completion:^
{

    SKAction *replaceAction = [SKAction customActionWithDuration:1
                                                     actionBlock:^(SKNode *node, CGFloat elapsedTime) 
    {
        self.explosion.position = DESIRED_POSITION;
    }];
}];
相关问题