如何在Sprite Kit中实现节切换?

时间:2018-05-16 09:57:26

标签: swift sprite-kit

我正在尝试在图像部分之间切换。为此,我做了不同的方法,如if语句,alpha 0/1,以及以下方法。

    enum SwitchState {
        case coins, lives, gems, default
    }

    func screenSettings(){
self.coinSection1 = self.childNode(withName: "//coinSection1") as? SKSpriteNode
        self.coinImage1 = self.childNode(withName: "//coinImage1") as? SKSpriteNode
        self.coin1BuyBtn = self.childNode(withName: "//coin1BuyBtn") as? SKSpriteNode
        self.coinAmount1 = self.childNode(withName: "//coinAmount1") as? SKLabelNode
        self.coinPrice1 = self.childNode(withName: "//coinPrice1") as? SKLabelNode

        self.coinSection2 = self.childNode(withName: "//coinSection2") as? SKSpriteNode
        self.coinImage2 = self.childNode(withName: "//coinImage2") as? SKSpriteNode
        self.coin2BuyBtn = self.childNode(withName: "//coin2BuyBtn") as? SKSpriteNode
        self.coinAmount2 = self.childNode(withName: "//coinAmount2") as? SKLabelNode
        self.coinPrice2 = self.childNode(withName: "//coinPrice2") as? SKLabelNode

        self.coinSection3 = self.childNode(withName: "//coinSection3") as? SKSpriteNode
        self.coinImage3 = self.childNode(withName: "//coinImage3") as? SKSpriteNode
        self.coin3BuyBtn = self.childNode(withName: "//coin3BuyBtn") as? SKSpriteNode
        self.coinAmount3 = self.childNode(withName: "//coinAmount3") as? SKLabelNode
        self.coinPrice3 = self.childNode(withName: "//coinPrice3") as? SKLabelNode
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)

        //Go to Settings Screen
        //Enabled Shop Coin Section
        if (coinButton?.contains(location))!{
            Sound.play(file: "switch", fileExtension: "mp3", numberOfLoops: 0)
            segmentControl(.coins)
        }

        //Enabled Shop Live Section
        if (liveButton?.contains(location))!{
            Sound.play(file: "switch", fileExtension: "mp3", numberOfLoops: 0)
            segmentControl(.lives)
        }

        //Enabled Shop Gem Section
        if (gemButton?.contains(location))!{
            Sound.play(file: "switch", fileExtension: "mp3", numberOfLoops: 0)
            segmentControl(.gems)
        }
    }
}

func segmentControl(_ sender: SwitchState ) {
    switch sender {
        case .coins:
            //Enable Coins
            shopCoinScreen?.isHidden = false
            coinImage1?.isHidden = false
            coinImage2?.isHidden = false
            coinImage3?.isHidden = false
            //Disable Lives
            shopLiveScreen?.isHidden = true
            coinImage1?.isHidden = true
            coinImage2?.isHidden = true
            coinImage3?.isHidden = true
            //Disable Gems
            shopGemScreen?.isHidden = true
            gemImage1?.isHidden = true
            gemImage2?.isHidden = true
            gemImage3?.isHidden = true

            print("coins")
        case .lives:
            //Disable Coins
            shopCoinScreen?.isHidden = true
            coinImage1?.isHidden = true
            coinImage2?.isHidden = true
            coinImage3?.isHidden = true
            //Enable Lives
            shopLiveScreen?.isHidden = false
            liveImage1?.isHidden = false
            liveImage2?.isHidden = false
            liveImage3?.isHidden = false
            //Disable Gems
            shopGemScreen?.isHidden = true
            gemImage1?.isHidden = true
            gemImage2?.isHidden = true
            gemImage3?.isHidden = true
            print("lives")
        case .gems:
            //Disable Coins
            shopCoinScreen?.isHidden = true
            coinImage1?.isHidden = true
            coinImage2?.isHidden = true
            coinImage3?.isHidden = true
            //Disable Lives
            shopLiveScreen?.isHidden = true
            liveImage1?.isHidden = true
            liveImage2?.isHidden = true
            liveImage3?.isHidden = true
            //Enable Gems
            shopGemScreen?.isHidden = false
            gemImage1?.isHidden = false
            gemImage2?.isHidden = false
            gemImage3?.isHidden = false
            print("gems")
        default:
            break
    }
}

它有时会切换图像,如果订单不同,有时只会显示相同的图像。打印输出显示实现正确。但是,我没能成功。

0 个答案:

没有答案