LoginSignup
3
3

More than 5 years have passed since last update.

SpriteKit入門 -15-

Posted at

ぴぽやさんがスプライトアニメーション用の素材を提供していましたので、試しに爆弾が爆発するアニメーションを作ってみます。

テクスチャにはこちらの画像を使用します。

bomb.png

タップした場所に爆弾を置いて、時間経過とともに爆発するアニメーションはこのような感じで実装できます。

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

実行結果はこのようになります。

spritekit_15_01.png

今回はここまで。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3