Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 影响按下按钮的功能_Ios_Iphone_Swift_Xcode_Touch - Fatal编程技术网

Ios 影响按下按钮的功能

Ios 影响按下按钮的功能,ios,iphone,swift,xcode,touch,Ios,Iphone,Swift,Xcode,Touch,我的游戏中有一个角色菜单,可以显示角色,并通过触摸移动功能从一侧移动到另一侧 我在场景中有一个按钮(灰色按钮),随着中心角色的变化,该按钮会变为某个按钮,具体取决于该角色是否解锁,从而确定将显示哪个按钮 我遇到的问题是,当我尝试按下按钮选择角色或按下按钮解锁角色时,需要多次按下才能使其工作,我在触摸结束功能中对按钮进行触摸,如下所示 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)

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

我在场景中有一个按钮(灰色按钮),随着中心角色的变化,该按钮会变为某个按钮,具体取决于该角色是否解锁,从而确定将显示哪个按钮

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

  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)

    }
override func touchesend(touch:Set,带有事件:UIEvent?){
让触摸:UITouch=touch.first!
让位置:CGPoint=touch.location(in:self)
let节点:SKNode=self.atPoint(位置)
让持续时间=0.25
如果节点==选择按钮{
setNewPlayerSprite(玩家名称:(centerPlayer?.name)!)
}如果节点==lockedButton | |节点==priceLabel,则为else{
解锁玩家(玩家名称:(centerPlayer?.name)!)
}如果节点==otherButton,则为else{
getSpecialUnlockData()
}如果节点==purchaseButton | |节点==PurchasePricelab,则为else{
purchaseCharacter(ID:characterProductID)
}如果节点==buyButton,则为else{
giveItemAmount(itemName:“Item2”,giveAmount:1)
}
如果我有他们在接触开始的方法,它工作得很好,没有任何问题,需要多次按下它才能让它工作。但如果玩家不小心碰了它,哪怕是轻微的,它就会解锁角色,这是一个问题,如果他们不想解锁角色或改变主意

触摸移动功能使菜单左右移动,这影响了我按下按钮的能力,因此需要多次按下按钮才能使其工作

如果按下其中一个按钮(或被按住),是否有办法阻止触动移动功能启动?这是我的完整触动功能代码。

  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)

    }
override func touchesMoved(touch:Set,带有事件:UIEvent?){
//让持续时间=0.01
让触摸:UITouch=touch.first!
让newPosition=touch.location(in:self)
让oldPosition=触摸上一个位置(in:self)
设xtransation=newPosition.x-oldPosition.x
如果centerPlayer!.frame.midX>size.width/2{
if(leftPlayer!=nil){
让actualTranslation=leftPlayer!.frame.midX+xtTranslation>leftGuide?xtTranslation:leftGuide-leftPlayer!.frame.midX
movePlayerByX(玩家:leftPlayer!,x:实际翻译)
}
}否则{
if(rightPlayer!=nil){
让actualTranslation=rightPlayer!.frame.midX+xtransation
我会用if语句检查触摸是否在特定视图中

大概是这样的:

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
}

}
override func touchesMoved(touch:Set,带有事件:UIEvent?){
//让持续时间=0.01
让触摸:UITouch=touch.first!
如果touch.view==self.view{
让newPosition=touch.location(in:self)
让oldPosition=触摸上一个位置(in:self)
设xtransation=newPosition.x-oldPosition.x
如果centerPlayer!.frame.midX>size.width/2{
if(leftPlayer!=nil){
让actualTranslation=leftPlayer!.frame.midX+xtTranslation>leftGuide?xtTranslation:leftGuide-leftPlayer!.frame.midX
movePlayerByX(玩家:leftPlayer!,x:实际翻译)
}
}否则{
if(rightPlayer!=nil){
让actualTranslation=rightPlayer!.frame.midX+xtransation
这有点……它可以在不能改变中间字符的情况下工作,但我不知道在按下某些按钮时如何检测它是否接触到它们。我可以对我的按钮使用相同的if语句吗?因为它们是spritenodes是的,我相信这篇文章有类似的答案