LoginSignup
3
3

More than 5 years have passed since last update.

SpriteKit入門 -14-

Posted at

今回は、自前の Texture Atlas がすでに用意されている場合に、どのようにテクスチャを使用するかを見ていきます。

例えば、このような画像ファイルがあるとします。

japanese-chess-02.jpg

指定した矩形部分だけを使用するにはこのようにします。

HelloScene.m
- (void)createSceneContents
{
    SKTexture *texture = [SKTexture textureWithImageNamed:@"japanese-chess-02.jpg"];
    CGFloat banWidth = 400.0;
    CGFloat banHeight = 320.0;
    CGFloat komaWidth = banWidth / 8.0;
    CGFloat komaHeight = banHeight / 6.0;
    for (int x=0; x < 8; x++) {
        for (int y=0; y < 6; y++) {
            if (((x+y)%3) == 1) {
                continue;
            }
            @autoreleasepool {
                SKTexture *texRect = [SKTexture textureWithRect:CGRectMake(x*komaWidth/banWidth, y*komaHeight/banHeight, komaWidth/banWidth, komaHeight/banHeight) inTexture:texture];
                SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:texRect];
                sprite.position = CGPointMake((x+1)*60, (y+1)*70);
                [self addChild:sprite];
            }
        }
    }
}

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

spritekit_14_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