Animate an SKSpriteNode with textures that have a size different from the original

前端 未结 1 1629
难免孤独
难免孤独 2021-01-15 02:03

I want to animate an SKSpriteNode using textures from an SKTextureAtlas, using SKAction.animateWithTextures(textures,timePerFrame,resize,restore). However, the

相关标签:
1条回答
  • 2021-01-15 02:53

    This would work

    1. Edit all the textures to match the size of the largest sized texture.

    Just give the smaller textures some padding using an alpha channel to give you a transparent background.

    E.g. Notice how the first texture has lots of negative space (From CartoonSmart.com)

    1. Create the physics body with a certain size in mind. E.g. You can load the texture without the padding and get the size. Then position it as needed onto the new and improved texture with padding. So after you create the Sprite as normal with the new resized textures you can then

      /// load a texture to be a template for the size
      let imageTextureSizeTemplate = SKTexture(imageNamed: textureWithoutPadding)
      
      let bodySize = imageTextureSizeTemplate.size()
      
       /// position template texture physics body on texture that will be used  
      let bodyCenter = CGPointMake(0.5, 0.5)
      
      // create physics body
      let body:SKPhysicsBody = SKPhysicsBody(rectangleOfSize: bodySize, center: bodyCeneter)
      
          self.physicsBody = body
      
    2. Set resize: false when you animate the textures.

    0 讨论(0)
提交回复
热议问题