문제

What Im Doing:

I have bombs being set every few seconds through a repeating Timer, continuously calling the following method.

//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];


}

What I want:

Is after the animation is over for the next sprite(explosion sprite) to appear in the location of where the bomb was.

The Problem:

There is no action I see to replace the sprite or aid in what I'm trying to do. SKAction Class Reference But I do see this task customActionWithDuration:actionBlock: but have no idea how to use it nor can find any examples through searches.

The Question:

Can someone show me how to use create this task to replace my bomb sprite with my explosion sprite after the 5 second bomb sprite animation is over? Thank you

도움이 되었습니까?

해결책

There is an SKAction that sets the sprite's texture to another one - so you don't even have to remove your bomb node in your sequence, just replace the texture. You can use that in a completion block, which will be executed once your action is finished.

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

Or, if you want the entire sprite to be put in there:

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

다른 팁

You can try this:

[self runAction:sequence
     completion:^
{

    SKAction *replaceAction = [SKAction customActionWithDuration:1
                                                     actionBlock:^(SKNode *node, CGFloat elapsedTime) 
    {
        self.explosion.position = DESIRED_POSITION;
    }];
}];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top