SKTexture
次のように、アトラスからロードされた別のテクスチャから作成された からスプライトを作成しています。
SKTexture *textureFromAtlas = [SKTexture textureWithImageNamed:@“MyImage.png”];
CGRect myRect = textureFromAtlas.textureRect;
myRect.size.with *= 0.5;
myRect.size.height *= 0.5;
SKTexture *newTexture = [SKTexture textureWithRect:myRect inTexture:textureFromAtlas];
SKSpriteNode * MySprite = [SKSpriteNode spriteNodeWithTexture:newTexture];
textureFromAtlas
アプリケーション バンドルのImages.atlascフォルダー内で、 Images.1.pngはMyImage.pngが回転したことを示し、 Images.plistはそのサブイメージに対してキーtextureRotatedが YES に設定されているため、たまたま回転しました。
MySprite
正しい回転になるように作成するにはどうすればよいですか?
(A)は、SpriteKit が自動回転を行うため、予想されるものです。 (B)は実際の結果です。つまり、上記のコードで得られるものです。
編集:
MyImage.pngのサイズを取得するためにいくつかのコードを追加する必要がありましたが、アトラスに含まれる回転されたものは、回転されているかどうかを確認し、新しい回転myRect
を計算して使用しますmySrpite
CGRect textureRect = textureFromAtlas.textureRect;
CGSize atlasSize = [[SKTexture textureWithRect:CGRectMake(0, 0, 1, 1)
inTexture:textureFromAtlas] size];
CGSize sizeInAtlas = CGSizeMake(textureRect.size.width * atlasSize.width,
textureRect.size.height * atlasSize.height);
myRect = CGRectMake(myRect.origin.x,
myRect.origin.y+myRect.size.height-sizeInAtlas.height/atlasSize.height);
SKTexture *rotatedTexture = [SKTexture textureWithRect:myRect
inTexture:textureFromAtlas];
mySprite = [SKSpriteNode spriteWithTexture: rotatedTexture];
mySprite.zRotation = M_PI_2;
これにより、四角形がImage.1.pngの回転した画像の左上隅に効果的に「移動」されるため、 (A)mySprite
の画像になりますが、これは MyImage.png が完全に不透明であるか、透明な境界線。SpriteKit は透明度のある画像でいくつかの最適化を行い、textureRect はアトラスの実際のフレームよりも小さいためです。