2
votes

So I've got an 'SKSpriteNode(texture: someTexture)' object that I run an 'SKAction' on which animates it (walking animation) with a couple of textures.

I set it's physicsBody using the texture parameter so that the physicsBody would be a perfect outline of my character, only thing is I can't quite figure out how to get the physicsBody to change as the texture changes (due to the animation).

I tried the obviously wrong choice of assigning the object a new SKPhysicsBody (in update method) with texture of object.texture (for current texture in animation), but that doesn't retain all the other properties I originally set. I searched for methods in the documentation for someway I could achieve this but found nothing.

What would be the best way to go about this?

1

1 Answers

1
votes

Normally you would design your game such that the same physics shape applies to all animation frames. It often does not matter at all whether a human character's limbs are all properly outlined in the shape, generally a rectangle often suffices.

Your approach was correct btw, you would have to create a new physics body every time and you would have to copy over the properties of the previous body before you replace it with a new one. However that is not a good solution anyway because the internal state of the body can't be preserved, and different shapes will not case the now-animating physics shapes to have proper physics behavior. For example if the character is swinging a bat, hitting the ball, it would basically just move the ball away from the bat but not apply any force from the swinging motion.

In essence what you are trying to achieve is not (really) possible. Instead give the node a shape that is a good compromise over all animation frames. Typically it's best to use the smallest possible shape. Alternatively you could compose the character of multiple sprites to be able to compose it of multiple (connected) bodies.