父节点移动时子节点不移动? [英] Child not moving when parent node moves?

查看:28
本文介绍了父节点移动时子节点不移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在世界上有三个节点,玩家和玩家视野".世界和愿景 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)
    }
}

推荐答案

问题是你的视觉节点由于它自己的physicsBody而受到物理引擎的影响,与它的父节点是分开的.解决方案是在 中将视觉节点的 position 属性设置为 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.

这篇关于父节点移动时子节点不移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆