Swift 如何删除单击的按钮,然后用另一个按钮替换,然后再次删除?

Swift 如何删除单击的按钮,然后用另一个按钮替换,然后再次删除?,swift,sprite-kit,Swift,Sprite Kit,好的,我试着这样做,当点击按钮1时,它会被移除,并被按钮2取代。当Button2被点击时,我希望它移除Button2并替换为button1,等等。。这是我到目前为止的代码(按钮是功能性的,所以这不是问题。) 以下是单击按钮1时运行的函数: func sound() { soundButton.removeFromParent() soundButton2.position = CGPoint(x: self.frame.size.width/2, y: self.frame.

好的,我试着这样做,当点击按钮1时,它会被移除,并被按钮2取代。当Button2被点击时,我希望它移除Button2并替换为button1,等等。。这是我到目前为止的代码(按钮是功能性的,所以这不是问题。)

以下是单击按钮1时运行的函数:

 func sound() {

    soundButton.removeFromParent()
    soundButton2.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height * 0.42)
    soundButton2.zPosition = 15
    self.addChild(soundButton2)

}
func sound1() {
    soundButton2.removeFromParent()

    self.addChild(soundButton)

}
以下是单击按钮2时运行的函数:

 func sound() {

    soundButton.removeFromParent()
    soundButton2.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height * 0.42)
    soundButton2.zPosition = 15
    self.addChild(soundButton2)

}
func sound1() {
    soundButton2.removeFromParent()

    self.addChild(soundButton)

}
最后,下面是我在touchesEnded函数中单击按钮的代码:

 //Sound1 Button Pressed
        for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if soundButton.containsPoint(location) {

            sound()

            for touch: AnyObject in touches {
                _ = touch.locationInNode(self)



            }


        }

    }

     //Sound1 Button Pressed
    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if soundButton2.containsPoint(location) {

        sound1()

            for touch: AnyObject in touches {
                _ = touch.locationInNode(self)



            }


        }

    }

我只需要使用
hidden
属性来切换两个按钮的可见性。将要显示的设置为
hidden=false
,将要隐藏的设置为
hidden=true

,这样会使此操作过于复杂。您需要的只是移除被点击的按钮,并添加一个合适的按钮:

import SpriteKit

class GameScene: SKScene{

    let soundButton = SKSpriteNode(color: UIColor.purpleColor(), size:CGSize(width: 200, height: 200))

     let soundButton2 = SKSpriteNode(color: UIColor.orangeColor(), size:CGSize(width: 200, height: 200))

    override func didMoveToView(view: SKView) {


        soundButton.name = "button1"
        soundButton2.name = "button2"

        soundButton.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
        soundButton.zPosition = 15
        self.addChild(soundButton)

        soundButton2.position = soundButton.position

    }

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

        let touch = touches.first

        if let location = touch?.locationInNode(self){

            let node = nodeAtPoint(location)

            if node.name == "button1" {

                node.removeFromParent()

                //Not really needed for this example, but a good habit
                if soundButton2.parent == nil {

                    addChild(soundButton2)
                }

            }else if node.name == "button2"{

                 node.removeFromParent()

                //Not really needed for this example, but a good habit
                if soundButton.parent == nil {

                    addChild(soundButton)
                }
            }
        }
    }
}
导入SpriteKit
类游戏场景:SKScene{
让soundButton=SKSpriteNode(颜色:UIColor.purpleColor(),大小:CGSize(宽度:200,高度:200))
让soundButton2=SKSpriteNode(颜色:UIColor.orangeColor(),大小:CGSize(宽度:200,高度:200))
覆盖func didMoveToView(视图:SKView){
soundButton.name=“button1”
soundButton2.name=“button2”
soundButton.position=CGPoint(x:CGRectGetMidX(帧),y:CGRectGetMidY(帧))
soundButton.zPosition=15
self.addChild(声音按钮)
soundButton2.position=soundButton.position
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
让我们先接触
如果让位置=触摸?.locationInNode(自身){
让节点=节点点(位置)
如果node.name==“button1”{
node.removeFromParent()节点
//这个例子并不需要,但需要一个好习惯
如果soundButton2.parent==nil{
addChild(声音按钮2)
}
}否则,如果node.name==“button2”{
node.removeFromParent()节点
//这个例子并不需要,但需要一个好习惯
如果soundButton.parent==nil{
addChild(声音按钮)
}
}
}
}
}
另一种方法是将一个
SKSpriteNode
子类化,使您的Button类(Button:SKSpriteNode),将其属性设置为true,并实现其
touchsbegind
/
touchseded
方法


或者,您可以使用具有许多不同功能的类(模仿UIButton的用途)。

但问题是,它们相互重叠,并且它们都检测用户是否触摸按钮,而不使用单个按钮和控制状态(或其他切换)来确定不同的(外观或)动作?假设我理解这个原因,你最好在按下时更改精灵的纹理和名称。然后,您可以检查精灵名称,并决定需要从那里做什么。