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