TouchesEnded 被称为其他按钮被点击并取消其他操作
TouchesEnded is called the other button is tapped and cancels other actions
当玩家按住一个按钮四处移动然后按下射击按钮时,会调用 TouchesEnded,然后取消玩家的移动。这两个操作单独工作,但同时调用它们时则不然。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if let touch = touches.first {
let location = touch.location(in: self)
let objects = nodes(at: location)
for node in objects {
if node.name == "leftBtn" {
player.run(player.leftMovement, withKey: "leftMovement")
} else if node.name == "rightBtn" {
player.run(player.rightMovement, withKey: "rightMovement")
} else if node.name == "upBtn" {
let jump = SKAction.applyImpulse(CGVector(dx: 0, dy: 1000), duration: 0.2)
player.run(jump, withKey: "jump")
} else if node.name == "downBtn" {
let downMovement = SKAction.applyImpulse(CGVector(dx: 0, dy: -500), duration: 0.2)
player.run(downMovement, withKey: "downMovement")
} else if node.name == "shootBtn" {
player.shoot()
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
player.removeAction(forKey: "leftMovement")
player.removeAction(forKey: "rightMovement")
player.removeAction(forKey: "jump")
player.removeAction(forKey: "downMovement")
}
我希望这两个动作独立于另一个动作,但不幸的是,情况并非如此。
这可能是因为当你触摸射击按钮时,touchesEnded
也会被调用,这将取消你的所有动作。
类似于您在 touchesBegan
方法中检查哪些节点被触摸的方式,您将需要检查在 touchesEnded
中是否按下了拍摄按钮:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let objects = nodes(at: location)
for node in objects {
if ["leftBtn", "rightBtn", "upBtn", "downBtn"].contains(node.name) {
player.removeAction(forKey: "leftMovement")
player.removeAction(forKey: "rightMovement")
player.removeAction(forKey: "jump")
player.removeAction(forKey: "downMovement")
}
}
}
当玩家按住一个按钮四处移动然后按下射击按钮时,会调用 TouchesEnded,然后取消玩家的移动。这两个操作单独工作,但同时调用它们时则不然。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if let touch = touches.first {
let location = touch.location(in: self)
let objects = nodes(at: location)
for node in objects {
if node.name == "leftBtn" {
player.run(player.leftMovement, withKey: "leftMovement")
} else if node.name == "rightBtn" {
player.run(player.rightMovement, withKey: "rightMovement")
} else if node.name == "upBtn" {
let jump = SKAction.applyImpulse(CGVector(dx: 0, dy: 1000), duration: 0.2)
player.run(jump, withKey: "jump")
} else if node.name == "downBtn" {
let downMovement = SKAction.applyImpulse(CGVector(dx: 0, dy: -500), duration: 0.2)
player.run(downMovement, withKey: "downMovement")
} else if node.name == "shootBtn" {
player.shoot()
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
player.removeAction(forKey: "leftMovement")
player.removeAction(forKey: "rightMovement")
player.removeAction(forKey: "jump")
player.removeAction(forKey: "downMovement")
}
我希望这两个动作独立于另一个动作,但不幸的是,情况并非如此。
这可能是因为当你触摸射击按钮时,touchesEnded
也会被调用,这将取消你的所有动作。
类似于您在 touchesBegan
方法中检查哪些节点被触摸的方式,您将需要检查在 touchesEnded
中是否按下了拍摄按钮:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let objects = nodes(at: location)
for node in objects {
if ["leftBtn", "rightBtn", "upBtn", "downBtn"].contains(node.name) {
player.removeAction(forKey: "leftMovement")
player.removeAction(forKey: "rightMovement")
player.removeAction(forKey: "jump")
player.removeAction(forKey: "downMovement")
}
}
}