gpt4 book ai didi

ios - 触摸影响按下按钮的移动功能

转载 作者:行者123 更新时间:2023-11-28 06:16:28 25 4
gpt4 key购买 nike

我的游戏中有这个角色菜单,它显示角色并通过触摸移动功能从一侧移动到另一侧。

enter image description here

我在 skscene 中有一个按钮(灰色按钮)随着中心字符的变化而变为某个按钮,具体取决于某个字符是否已解锁决定将显示哪个按钮。

我遇到的问题是,当我尝试按下按钮选择字符或按下解锁字符时,需要多次按下才能使其工作,我在触摸结束功能中触摸了按钮,如下所示.

  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

let touch: UITouch = touches.first!
let location: CGPoint = touch.location(in: self)
let node: SKNode = self.atPoint(location)
let duration = 0.25

if node == selectButton {

setNewPlayerSprite(nameOfPlayer: (centerPlayer?.name)!)

} else if node == lockedButton || node == priceLabel {

unlockPlayer(nameOfPlayer: (centerPlayer?.name)!)

} else if node == otherButton {

getSpecialUnlockData()

} else if node == purchaseButton || node == purchasePriceLabe {

purchaseCharacter(ID: characterProductID)

} else if node == buyButton {

giveItemAmount(itemName: "Item2", giveAmount: 1)

}

如果我在 touches begins 方法中使用它们,它可以正常工作,无需多次按下它即可正常工作。但是,如果玩家不小心触摸到它,即使是轻微的触摸也会解锁角色,如果他们不想解锁角色或改变主意,这就是一个问题。

使菜单从一侧移动到另一侧的触摸移动功能影响了我按下按钮的能力,因此导致我需要多次按下按钮才能使其正常工作。

如果按下其中一个按钮,是否有某种方法可以阻止触摸移动功能的触发? (或按住)这是我的完整触摸功能代码。

  override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

//let duration = 0.01
let touch: UITouch = touches.first!
let newPosition = touch.location(in: self)
let oldPosition = touch.previousLocation(in: self)
let xTranslation = newPosition.x - oldPosition.x

if centerPlayer!.frame.midX > size.width/2 {
if (leftPlayer != nil) {
let actualTranslation = leftPlayer!.frame.midX + xTranslation > leftGuide ? xTranslation : leftGuide - leftPlayer!.frame.midX
movePlayerByX(player: leftPlayer!, x: actualTranslation)
}
} else {
if (rightPlayer != nil) {
let actualTranslation = rightPlayer!.frame.midX + xTranslation < rightGuide ? xTranslation : rightGuide - rightPlayer!.frame.midX
movePlayerByX(player: rightPlayer!, x: actualTranslation)
}
}

movePlayerByX(player: centerPlayer!, x: xTranslation)
priceLabel.isHidden = true; selectButton.isHidden = true; lockedButton.isHidden = true; otherButton.isHidden = true
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

let touch: UITouch = touches.first!
let location: CGPoint = touch.location(in: self)
let node: SKNode = self.atPoint(location)
let duration = 0.25

if node == selectButton {

setNewPlayerSprite(nameOfPlayer: (centerPlayer?.name)!)

} else if node == lockedButton || node == priceLabel {

unlockPlayer(nameOfPlayer: (centerPlayer?.name)!)

} else if node == otherButton {

getSpecialUnlockData()

} else if node == purchaseButton || node == purchasePriceLabe {

purchaseCharacter(ID: characterProductID)

} else if node == buyButton {

giveItemAmount(itemName: "Item2", giveAmount: 1)

}

最佳答案

我会使用 if 语句检查触摸是否在特定 View 中。

像这样:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

//let duration = 0.01
let touch: UITouch = touches.first!
if touch.view == self.view{
let newPosition = touch.location(in: self)
let oldPosition = touch.previousLocation(in: self)
let xTranslation = newPosition.x - oldPosition.x

if centerPlayer!.frame.midX > size.width/2 {
if (leftPlayer != nil) {
let actualTranslation = leftPlayer!.frame.midX + xTranslation > leftGuide ? xTranslation : leftGuide - leftPlayer!.frame.midX
movePlayerByX(player: leftPlayer!, x: actualTranslation)
}
} else {
if (rightPlayer != nil) {
let actualTranslation = rightPlayer!.frame.midX + xTranslation < rightGuide ? xTranslation : rightGuide - rightPlayer!.frame.midX
movePlayerByX(player: rightPlayer!, x: actualTranslation)
}
}

movePlayerByX(player: centerPlayer!, x: xTranslation)
priceLabel.isHidden = true; selectButton.isHidden = true;
lockedButton.isHidden = true; otherButton.isHidden = true
}

}

关于ios - 触摸影响按下按钮的移动功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44985116/

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