Does the child not move when the parent node moves?

I have three nodes: the world, the player, and the "player vision". Both the world and the vision of SKShapeNodes and my player use their own subclass of SKShapeNode. When I move the world, the whole player moves with him, however, when I move the player, the vision of the node remains fixed in his position. What could be the reason for this?

This is my player class:

class Character : SKShapeNode { var vision : SKShapeNode var spinning = false init(size: CGSize) { vision = SKShapeNode() // Player Shape super.init() self.path = SKShapeNode(rectOfSize: size).path self.fillColor = SKColor.blackColor() self.strokeColor = SKColor.blackColor() self.name = "Player" // Player Physics Body self.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size) self.physicsBody.restitution = 0 self.physicsBody.allowsRotation = false self.physicsBody.categoryBitMask = ColliderType.Player.toRaw() self.physicsBody.collisionBitMask = ColliderType.Wall.toRaw() self.physicsBody.contactTestBitMask = ColliderType.Wall.toRaw() | ColliderType.Player.toRaw() | ColliderType.Enemy.toRaw() // Vision Shape vision = SKShapeNode(rectOfSize: CGSize(width: 200, height: 1)) vision.fillColor = SKColor.greenColor() // Vision Physics body vision.physicsBody = SKPhysicsBody(rectangleOfSize: vision.frame.size) vision.physicsBody.affectedByGravity = false vision.physicsBody.categoryBitMask = ColliderType.Vision.toRaw() vision.physicsBody.collisionBitMask = 0 vision.physicsBody.contactTestBitMask = ColliderType.Wall.toRaw() self.addChild(vision) } } 
+1
source share
1 answer

The problem is that your node vision affects the physics engine separately from its parent due to its own physics. The solution is to set the node position vision to CGPoint(0, 0) (or what you want it to be in its original coordinate system) in the didSimulatePhysics step of the frame. This will restore the view of the node to “follow” the parent node at any time.

+2
source

Source: https://habr.com/ru/post/972535/


All Articles