2

I am building my first sprite kit game, and recently read an article that said to use SKTextureAtlas to improve performance.

So I migrated all my sprite images into organized .atlas folders and updated the code accordingly. However, now my sprites have weird physics bodies (mainly over-enlarged).

For instance, my player sprite has three states: Flying (flat), Flying Up and Flying Down. The three images are pretty similar, with slight variations (arms pointing flat, up, down).

Before migrating to texture atlases, the physics body was pretty spot on with the image. Now however, it is much larger than the image and slightly stretched on the y-axis.

Here is my player.atlas file structure:

player-flying-iphone.atlas:

  • player-flying-down@2x.png
  • player-flying-down@3x.png
  • player-flying-up@2x.png
  • player-flying-up@3x.png
  • player-flying@2x.png
  • player-flying@3x.png

Here is an example of my code: (Player is a subclassed SKSpriteNode)

let textureAtlas = SKTextureAtlas(named: "player-iphone")
let playerFlyingTexture = textureAtlas.textureNamed("player-flying")
let player = Player(texture: playerFlyingTexture, color: UIColor.clearColor(), size: CGSizeMake(100.0, 100.0))

let physicsBody = SKPhysicsBody(texture: playerFlyingTexture, size: CGSizeMake(100.0, 100.0))
physicsBody.dynamic = true
physicsBody.affectedByGravity = true
physicsBody.usesPreciseCollisionDetection = false
physicsBody.categoryBitMask = CollisionCategories.Player
physicsBody.contactTestBitMask = CollisionCategories.Enemy
physicsBody.collisionBitMask = CollisionCategories.EdgeBody
physicsBody.allowsRotation = false
player.physicsBody = physicsBody
4

3 に答える 3

1

次のようにテクスチャ サイズを設定しています。

CGSize(widht:100, height:100)

これは明らかにテクスチャの正しい寸法ではありません。

代わりに実際のテクスチャ サイズを使用します。

yourTexture.size()

于 2016-04-13T10:16:46.040 に答える
0

この問題は iPad 3 (iOS 9.3.5 & xcode 8.1) に存在し、私の状況では、alphaThreshold は間違った PhysicsBody の問題を解決しませんでした。

「Texture Atlas」ではなく「New Sprite Atlas」に切り替える必要があり、魔法のように問題が修正されました。

于 2016-11-08T08:51:22.557 に答える