gpt4 book ai didi

ios - TouchesEnded 被称为其他按钮被点击并取消其他操作

转载 作者:行者123 更新时间:2023-11-28 13:31:48 26 4
gpt4 key购买 nike

当玩家按住按钮四处移动然后按下射击按钮时,会调用 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 也会被调用,这将取消您的所有 Action 。

与您在 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")
}
}
}

关于ios - TouchesEnded 被称为其他按钮被点击并取消其他操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57241298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com