Physics Body

How to update physicsBody whenever the texture while playing an animation?

func createPlayer() {
    player = childNode(withName: "player") as! SKSpriteNode
  
  player.position = CGPoint(x: mapNode.tileSize.width, y: mapNode.tileSize.height*3.5)

    var Frames: [SKTexture]

    Frames = runAnimationHelper.loadTextures(from: SKTextureAtlas(named: "knight_front_ideal"), withName: "knight_front_ideal")

    player.run(SKAction.repeatForever(SKAction.animate(with: Frames, timePerFrame: 0.10, resize: false, restore: true)))

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

    player.physicsBody?.isDynamic = true
     
  }

Using the above code player have the initial texture physicsBody and not updating with the animation.

Also if I update the physicBody in "update" function of swift then there's a lag of a second which is not acceptable.

Two approaches come to mind:

  1. You could use the SKSceneDelegate method didEvaluateActions(for:), where you could change the physicsBody to the body that corresponds with the current texture of the node.

  2. You could create a custom action using customAction(withDuration:actionBlock:), where you could handle both cycling the texture and the physics body.

Physics Body
 
 
Q