ios - TouchesEnded 被称为另一个按钮被点击并取消其他动作
问题描述
当玩家按住一个按钮移动然后按下射击按钮时,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")
}
}
}
推荐阅读
- python - 使用 periodindex 在具有多索引列的数据帧上进行 GroupBy
- windows - 按住 alt 键时如何防止 Windows 激活键提示?
- python - wx.KeyEvent 和非 QWERTY 键盘
- android - 引用泛型类型 T 的实例
- javascript - 不同对象的映射列表
- python - 当某些值是无数据类型时,如何编写一个函数来将列表列表中的值与额外条件进行比较
- erlang - 从地图创建多个进程
- android - Android Studio“找不到aapt2-windows.jar”错误
- git - 列出在 Git 存储库的某个分支中存在的文件夹的所有子文件夹
- dialogflow-es - Google 助理意图响应中的 SSML MalformedRequest 错误