ぴぽやさんがスプライトアニメーション用の素材を提供していましたので、試しに爆弾が爆発するアニメーションを作ってみます。
テクスチャにはこちらの画像を使用します。
タップした場所に爆弾を置いて、時間経過とともに爆発するアニメーションはこのような感じで実装できます。
HelloScene.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
int type = rand() % 5;
SKTexture *texture = [SKTexture textureWithImageNamed:@"bomb.png"];
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:[self newBomTexture:texture x:0 y:type]];
sprite.position = location;
[self addChild:sprite];
SKAction *action = [self newBomAction:type texture:texture];
[sprite runAction:action completion:^{
[sprite removeFromParent];
}];
}
- (SKAction *) newBomAction:(int)type texture:(SKTexture *)texture
{
SKAction *action = nil;
NSMutableArray *array = [NSMutableArray array];
for (int x=0; x < 6; x++) {
[array addObject:[self newBomTexture:texture x:x y:9-type]];
}
for (int x=0; x < 6; x++) {
[array addObject:[self newBomTexture:texture x:x y:9-(type+5)]];
}
action = [SKAction animateWithTextures:[array copy] timePerFrame:0.2];
return action;
}
- (SKTexture *) newBomTexture:(SKTexture *)texture x:(int)x y:(int)y
{
CGFloat pngWidth = 192.0;
CGFloat pngHeight = 320.0;
CGFloat rectWidth = pngWidth / 6.0;
CGFloat rectHeight = pngHeight / 10.0;
return [SKTexture textureWithRect:CGRectMake(x*rectWidth/pngWidth, y*rectHeight/pngHeight, rectWidth/pngWidth, rectHeight/pngHeight) inTexture:texture];
}
実行結果はこのようになります。
今回はここまで。