微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

相机随节点移动后无法按下节点

如何解决相机随节点移动后无法按下节点

我可以让我的控制按钮随着相机移动,但是在玩家移动后它们不再起作用。我认为它们的位置随节点移动,因此在触摸开始时,位置在被触摸时仍然有效。玩家停止移动后我还应该更新位置吗?相机节点随玩家移动。

override func didMove(to view: SKView) {
    self.anchorPoint = CGPoint(x: 0.5,y: 0.5)

    // Make sure you can get teh player from the scene file
    if let somePlayer = self.childNode(withName: "player") as? SKSpriteNode {
        player = somePlayer
    
        // Set physics
        player.physicsBody?.isDynamic = false

    } else {
        print("No player")
    }
    
    
    let widthHalf:CGFloat = self.view!.bounds.width / 2
    let heightHalf:CGFloat = self.view!.bounds.height / 2
    
    cameraNode.addChild(buttonnorth)
    buttonnorth.position = CGPoint(x: -widthHalf + 80,y: -heightHalf + 100)
    
    cameraNode.addChild(buttonSouth)
    buttonSouth.position = CGPoint(x: -widthHalf + 80,y: -heightHalf + 40)
    buttonSouth.yScale = -1
    
    cameraNode.addChild(buttonWest)
    buttonWest.position = CGPoint( x: -widthHalf + 30,y: -heightHalf + 70)
    
    cameraNode.addChild(buttonEast)
    buttonEast.position = CGPoint( x: -widthHalf + 130,y: -heightHalf + 70)
    
    buttonnorth.xScale = 0.4
    buttonnorth.yScale = 0.4
    
    buttonSouth.xScale = 0.4
    buttonSouth.yScale = 0.4
    buttonSouth.zRotation = CGFloat(Double.pi)
    
    buttonEast.xScale = 0.4
    buttonEast.yScale = 0.4
    buttonEast.zRotation = CGFloat(Double.pi)
    
    buttonWest.xScale = 0.4
    buttonWest.yScale = 0.4
    
    addChild(cameraNode)
    camera = cameraNode
    
}

   override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
        
        for touch in (touches ) {
            let location = touch.location(in: self)
            
            
            if (buttonnorth.frame.contains(location)) {
                
                currentState = MoveStates.n
                buttonnorth.texture = SKTexture(imageNamed: "Directional_Button_Lit")
                isPressing = true
                
            } else if (buttonSouth.frame.contains(location)) {
                
                currentState = MoveStates.s
                buttonSouth.texture = SKTexture(imageNamed: "Directional_Button_Lit")
                isPressing = true
                
            }  else if (buttonEast.frame.contains(location)) {
                
                currentState = MoveStates.e
                buttonEast.texture = SKTexture(imageNamed: "Directional_Button2_Lit")
                isPressing = true
                
            }  else if (buttonWest.frame.contains(location)) {
                
                currentState = MoveStates.w
                buttonWest.texture = SKTexture(imageNamed: "Directional_Button2_Lit")
                isPressing = true
                
            }
            
        }
    }

解决方法

问题出在我的接触开始

我有这个:

let location = touch.location(in: self)

但是因为我让节点随着相机移动,所以我需要:

let location = touch.location(in: self.camera!)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。