当父节点移动时,孩子不动?

时间:2014-07-25 22:35:25

标签: ios sprite-kit

世界上有三个节点,玩家和“玩家愿景”。世界和视觉SKShapeNodes和我的播放器都使用SKShapeNode的自定义子类。当我移动世界时,所有玩家都随之移动,但是当我移动玩家时,视觉节点保持固定在它的位置。这可能是什么原因?

这是我的玩家类:

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 个答案:

答案 0 :(得分:2)

问题在于,您的视觉节点由物理引擎与其父节点分开影响,因为它有自己的物理实体。解决方案是在position步骤中将视觉节点的CGPoint(0, 0)属性设置为didSimulatePhysics(或者您希望它在其父坐标系中的任何内容)的框架。这将导致将视觉节点重置为"关注"始终是父节点。