今回は、自前の Texture Atlas がすでに用意されている場合に、どのようにテクスチャを使用するかを見ていきます。
例えば、このような画像ファイルがあるとします。
指定した矩形部分だけを使用するにはこのようにします。
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];
}
}
}
}
実行結果はこのようになります。
今回はここまで。