问题描述
世界上有三个节点,玩家和玩家愿景。世界和视觉SKShapeNodes和我的播放器都使用SKShapeNode的自定义子类。当我移动世界时,所有玩家都随之移动,但是当我移动玩家时,视觉节点保持固定在它的位置。可能是什么原因?
I have three nodes the world, the player and "the player vision". Both the world and the vision SKShapeNodes and my player uses a custom subclass of SKShapeNode. When I move the world all the player moves with it, however when I move the player the vision node stays fixed in it's position. What could be the reason for this?
这是我的玩家类:
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)
}
}
推荐答案
问题是你的视觉节点由物理引擎与其父节点分开影响,因为它有自己的物理实体。解决方案是将视觉节点的位置
属性设置为 CGPoint(0,0)
(或者您希望的任何内容)在框架的 didSimulatePhysics
步骤中,在其父坐标系中。这将导致重置视觉节点以始终跟随父节点。
The problem is that your vision node is affect by the physics engine separately from its parent due to its own physicsBody. The solution is to set the vision node's position
property to CGPoint(0, 0)
(or whatever you desire it to be in its parent's coordinate system) in the didSimulatePhysics
step of the frame. This will have the result of resetting the vision node to "follow" the parent node at all times.
这篇关于当父节点移动时,孩子没有移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!