Search code examples
iosswiftsprite-kitskphysicsbody

Physics body for animated node on Sprite Kit


I have an animated character on my game with 8 different textures that are on "repeat forever". I need a physics body for it but I don't know how I can create. In my other games I use player.physicsBody = SKPhysicsBody(rectangleOf: UI.player.size) but now I want to use:

player.physicsBody = SKPhysicsBody(texture: playerTexture, size: player.size)

The problem is the textures are different and I can't keep track of them. Any ideas?

This is the animation code:

func playerRunning(player: SKSpriteNode){
        let texture1 = SKTexture(imageNamed: "DJ 1")
        let texture2 = SKTexture(imageNamed: "DJ 2")
        let texture3 = SKTexture(imageNamed: "DJ 3")
        let texture4 = SKTexture(imageNamed: "DJ 4")
        let texture5 = SKTexture(imageNamed: "DJ 5")
        let texture6 = SKTexture(imageNamed: "DJ 6")
        let texture7 = SKTexture(imageNamed: "DJ 7")
        let texture8 = SKTexture(imageNamed: "DJ 8")
        let animate = SKAction.animate(with: [texture1, texture2, texture3, texture4, texture5, texture6, texture7, texture8], timePerFrame: 0.125)
        let animateRF = SKAction.repeatForever(animate)
        player.run(animateRF, withKey:"runningAction")
    }

Solution

  • I do not know of any way to make the physics body change to match each texture in the animation sequence. Either pick one of the 8 textures as the texture to create the physics body or use a rectangle to create the physics body like you did in your other games.