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

时间:2016-01-12 19:35:08

标签: swift sprite-kit

好的,所以当我点击Button1时,我试图这样做,它会被删除并被Button2取代。当点击Button2时,我希望它删除button2并将其替换为button1等。这是我到目前为止的代码(按钮功能正常,这不是问题。)

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

这里是单击Button1时运行的功能:

 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)

}

这是点击Button2时运行的功能:

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)



            }


        }

    }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你这太复杂了。你需要的只是删除被点击的按钮,并添加一个合适的按钮:

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)
                }
            }
        }
    }
}

另一种方式是子类化SKSpriteNode并创建Button类(Button:SKSpriteNode),将其userInteractionEnabled属性设置为true,并实现其touchesBegan / {{ 1}}方法。

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