Swift 如何在Sprite套件中实现区段切换?

Swift 如何在Sprite套件中实现区段切换?,swift,sprite-kit,Swift,Sprite Kit,我试图在图像部分之间切换。为此,我使用了不同的方法,比如if语句、alpha0/1以及下面的方法 enum SwitchState { case coins, lives, gems, default } func screenSettings(){ self.coinSection1 = self.childNode(withName: "//coinSection1") as? SKSpriteNode self.coinImage1

我试图在图像部分之间切换。为此,我使用了不同的方法,比如if语句、alpha0/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
    }
}
enum开关状态{
案例硬币、生命、宝石、默认值
}
func屏幕设置(){
self.coinSection1=self.childNode(名称://coinSection1”)作为?SKSpriteNode
self.coinImage1=self.childNode(名称://coinImage1”)作为?SKSpriteNode
self.coin1BuyBtn=self.childNode(名称://coin1BuyBtn”)作为?SKSpriteNode
self.coinAmount1=self.childNode(名称://coinAmount1”)作为?SKLabelNode
self.coinPrice1=self.childNode(名称://coinPrice1”)作为?SKLabelNode
self.coinSection2=self.childNode(名称://coinSection2”)作为?SKSpriteNode
self.coinImage2=self.childNode(名称://coinImage2”)作为?SKSpriteNode
self.coin2BuyBtn=self.childNode(名称://coin2BuyBtn”)作为?SKSpriteNode
self.coinAmount2=self.childNode(名称://coinAmount2”)作为?SKLabelNode
self.coinPrice2=self.childNode(名称://coinPrice2”)作为?SKLabelNode
self.coinSection3=self.childNode(名称://coinSection3”)作为?SKSpriteNode
self.coinImage3=self.childNode(名称://coinImage3”)作为?SKSpriteNode
self.coin3BuyBtn=self.childNode(名称://coin3BuyBtn”)作为?SKSpriteNode
self.coinAmount3=self.childNode(名称://coinAmount3”)作为?SKLabelNode
self.coinPrice3=self.childNode(名称://coinPrice3”)作为?SKLabelNode
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
//转到设置屏幕
//商店硬币组
如果(coinButton?.contains(location)){
播放(文件:“开关”,文件扩展名:“mp3”,numberOfLoops:0)
分段控制(.coins)
}
//已启用的车间现场部分
如果(liveButton?.contains(location)){
播放(文件:“开关”,文件扩展名:“mp3”,numberOfLoops:0)
分段控制(.lives)
}
//启用店铺宝石组
如果(gemButton?包含(位置)){
播放(文件:“开关”,文件扩展名:“mp3”,numberOfLoops:0)
分段控制(.gems)
}
}
}
func段控制(\发送方:开关状态){
开关发送器{
案例.硬币:
//启用硬币
shopCoinScreen?.isHidden=false
coinImage1?.isHidden=false
coinImage2?.isHidden=false
coinImage3?.isHidden=false
//残废生命
shopLiveScreen?.ishiden=true
coinImage1?.isHidden=true
coinImage2?.isHidden=true
coinImage3?.isHidden=true
//禁用宝石
shopGemScreen?.ishiden=true
geimage1?.ishiden=真
geimage2?.ishiden=true
geimage3?.ishiden=真
印刷品(“硬币”)
案例.生命:
//禁用硬币
shopCoinScreen?.ishiden=true
coinImage1?.isHidden=true
coinImage2?.isHidden=true
coinImage3?.isHidden=true
//救命
shopLiveScreen?.isHidden=false
liveImage1?.isHidden=false
liveImage2?.isHidden=false
liveImage3?.isHidden=false
//禁用宝石
shopGemScreen?.ishiden=true
geimage1?.ishiden=真
geimage2?.ishiden=true
geimage3?.ishiden=真
打印(“生活”)
案例.宝石:
//禁用硬币
shopCoinScreen?.ishiden=true
coinImage1?.isHidden=true
coinImage2?.isHidden=true
coinImage3?.isHidden=true
//残废生命
shopLiveScreen?.ishiden=true
liveImage1?.isHidden=true
liveImage2?.isHidden=true
liveImage3?.isHidden=true
//启用Gems
shopGemScreen?.ishiden=错误
geimage1?.ishiden=错误
gemImage2?.ishiden=false
geimage3?.ishiden=错误
印刷品(“宝石”)
违约:
打破
}
}

它有时切换图像,如果顺序不同,有时只显示相同的图像。打印输出表明实现是正确的。但是,我没能做到。

你能在你的问题中添加代码到你的代码中创建对象(GeImage1、liveImage1等)的位置吗?当然,我刚刚添加了另一部分。因为你是通过场景编辑器添加项目的,你能肯定它们具有正的和正确的zPosition值SzPositions是2great!当你自己弄明白的时候就更好了。