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

扶醉桌前 提交于 2019-12-01 12:44:51

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!