目前,我可以使用两个按钮进行跳跃和移动。它们是SKSpriteNodes,我使每个动作都在被触摸时发生。我面临的问题是,当我按下一个按钮时,该动作起作用,而当我按下另一个按钮时,另一个动作停止,因为不再触摸该按钮。我正在尝试允许用户同时移动和跳跃。例如,按住左按钮并按下跳转按钮。
// MARK: Touches
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointTouched = touch.location(in: cameraNode)
if leftArrow.contains(pointTouched)
{
leftArrow.alpha = 0.5
moveWizard(moveBy: -300, forTheKey: "moveX")
wizardRunAnimation()
wizardCharacter.xScale = -1
}
if rightArrow.contains(pointTouched)
{
rightArrow.alpha = 0.5
moveWizard(moveBy: 430, forTheKey: "moveX")
wizardRunAnimation()
wizardCharacter.xScale = 1
}
if attackButton.contains(pointTouched)
{
attackButton.alpha = 0.5
wizardAttackAnimation()
}
if upArrow.contains(pointTouched)
{
upArrow.alpha = 0.5
jumpWizard(forTheKey: "jumped")
jumpWizardAnimation(fortheKey: "jumpedAnimation")
}
}
}
func alpha() {
leftArrow.alpha = 1
rightArrow.alpha = 1
attackButton.alpha = 1
upArrow.alpha = 1
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointTouched = touch.location(in: cameraNode)
if leftArrow.contains(pointTouched)
{
wizardCharacter.removeAction(forKey: "moveX")
if rightArrow.contains(pointTouched)
{
wizardCharacter.removeAction(forKey: "moveX")
}
if attackButton.contains(pointTouched)
{
wizardCharacter.removeAction(forKey: "wizardRun")
}
if upArrow.contains(pointTouched)
{
wizardCharacter.removeAction(forKey: "jumped")
}
}