LoginSignup
2
2

More than 5 years have passed since last update.

SpriteKit入門 -6-

Last updated at Posted at 2013-10-25

前回までで真っ黒い画面に遷移しましたが、
今回は、spaceshipスプライトを追加します。

SpaceshipScene.m
- (void)createSceneContents
{
    self.backgroundColor = [SKColor blackColor];
    self.scaleMode = SKSceneScaleModeAspectFit;
    SKSpriteNode *spaceship = [self newSpaceship];
    spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)-150);
    [self addChild:spaceship];
}

newSpaceshipメソッドを実装します。
アニメーションする灰色の四角を作成しています。

SpaceshipScene.m
- (SKSpriteNode *)newSpaceship
{
    SKSpriteNode *hull = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(64,32)];
    SKAction *hover = [SKAction sequence:@[
                                           [SKAction waitForDuration:1.0],
                                           [SKAction moveByX:100 y:50.0 duration:1.0],
                                           [SKAction waitForDuration:1.0],
                                           [SKAction moveByX:-100.0 y:-50 duration:1.0]]];
    [hull runAction:[SKAction repeatActionForever:hover]];
    return hull;
}

実行すると、このように灰色の四角が移動します。

実行結果

newSpaceshipメソッドに下記コードを追加します。
ライトを2つくっつけます。

SpaceshipScene.m
    SKSpriteNode *light1 = [self newLight];
    light1.position = CGPointMake(-28.0, 6.0);
    [hull addChild:light1];
    SKSpriteNode *light2 = [self newLight];
    light2.position = CGPointMake(28.0, 6.0);
    [hull addChild:light2];

newLightメソッドを実装します。
ライトは黄色く点滅する四角で表現しています。

SpaceshipScene.m
- (SKSpriteNode *)newLight
{
    SKSpriteNode *light = [[SKSpriteNode alloc] initWithColor:[SKColor yellowColor] size:CGSizeMake(8,8)];
    SKAction *blink = [SKAction sequence:@[
                                           [SKAction fadeOutWithDuration:0.25],
                                           [SKAction fadeInWithDuration:0.25]]];
    SKAction *blinkForever = [SKAction repeatActionForever:blink];
    [light runAction:blinkForever];
    return light;
}

実行すると、灰色の四角に点滅する黄色い四角がくっついています。
黄色い四角は灰色の四角の子要素として追加しているので、
黄色の四角の座標は、灰色の四角からの相対座標になっています。

実行結果

今回はここまで。

2
2
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
2
2