SKSpriteNode的Alpha不变

时间:2018-07-16 05:33:10

标签: swift alpha touchesbegan touchesmoved touchesended

在GameOver场景中,我有四个按钮。除了restartBtn以外,所有三个按钮(homeBtn,rewardAdBtn和gameCenterBtn)的Alpha值均已正确更改。此按钮完全不变。

他们被这样初始化:

var homeBtn = SKSpriteNode()
var restartBtn = SKSpriteNode()
var rewardAdBtn = SKSpriteNode()
var gameCenterBtn = SKSpriteNode()

在加载视图中,我像这样设置它们(使用.sks文件设置屏幕)

override func didMove(to view: SKView) {

    homeBtn = self.childNode(withName: "homeBtn") as! SKSpriteNode
    homeBtn.name = "homeBtn"

    restartBtn = self.childNode(withName: "restartBtn") as! SKSpriteNode
    restartBtn.name = "restartBtn"

    rewardAdBtn = self.childNode(withName: "rewardAdBtn") as! SKSpriteNode
    rewardAdBtn.name = "rewardAdBtn"

    gameCenterBtn = self.childNode(withName: "gameCenterBtn") as! SKSpriteNode
    gameCenterBtn.name = "gameCenterBtn"
}

我的目标是模拟被单击的按钮。按下按钮后,字母将更改为node.alpha = 0.25。这是touchesBegantouchesMovedtouchesEnded中的代码。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touch:UITouch = (touches.first as UITouch?)!
    let touchLocation = touch.location(in: self)
    let touchedNode = self.atPoint(touchLocation)

    if let name = touchedNode.name {
        if name == "homeBtn" {
            homeBtn.alpha = 0.25
        }else if name == "restartBtn" {
            restartBtn.alpha = 0.25
        }else if name == "rewardAdBtn" {
            rewardAdBtn.alpha = 0.25
        }else if name == "gameCenterBtn" {
            gameCenterBtn.alpha = 0.25
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touch:UITouch = (touches.first as UITouch?)!
    let touchLocation = touch.location(in: self)
    let touchedNode = self.atPoint(touchLocation)

    if let name = touchedNode.name {
        if name == "homeBtn" {
            homeBtn.alpha = 0.25
            restartBtn.alpha = 1
            rewardAdBtn.alpha = 1
            gameCenterBtn.alpha = 1
        }else if name == "restartBtn" {
            homeBtn.alpha = 1
            restartBtn.alpha = 0.25
            rewardAdBtn.alpha = 1
            gameCenterBtn.alpha = 1
        }else if name == "rewardAdBtn" {
            homeBtn.alpha = 1
            restartBtn.alpha = 1
            rewardAdBtn.alpha = 0.25
            gameCenterBtn.alpha = 1
        }else if name == "gameCenterBtn" {
            homeBtn.alpha = 1
            restartBtn.alpha = 1
            rewardAdBtn.alpha = 1
            gameCenterBtn.alpha = 0.25
        }else{
            homeBtn.alpha = 1
            restartBtn.alpha = 1
            rewardAdBtn.alpha = 1
            gameCenterBtn.alpha = 1
        }
    }
}

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

    let touch:UITouch = (touches.first as UITouch?)!
    let touchLocation = touch.location(in: self)
    let touchedNode = self.atPoint(touchLocation)

    if let name = touchedNode.name {

        let currWidth = screenWidth
        let currHeight = screenHeight
        let transition = SKTransition.fade(with: UIColor.white, duration: 0.5)

        if name == "homeBtn" {
            playButtonSE()
            let scene = StartScene(fileNamed: "StartScene")
            scene?.size = CGSize(width: currWidth, height: currHeight)
            scene?.scaleMode = .aspectFill
            self.view?.presentScene(scene!, transition: transition)
        }else if name == "restartBtn" {
            playButtonSE()
            defaults.set(true, forKey: "canCont")
            let scene = GameScene(size: CGSize(width: currWidth, height: currHeight))
            scene.scaleMode = .aspectFill
            self.view?.presentScene(scene, transition: transition)

        }else if name == "rewardAdBtn" && defaults.bool(forKey: "canCont") == true{
            if(rewardVideo.isReady){
                playButtonSE()
                let currentViewController:UIViewController=UIApplication.shared.keyWindow!.rootViewController!
                rewardVideo.present(fromRootViewController: currentViewController)
            }
        }else if name == "gameCenterBtn" {
            playButtonSE()
            showLeaderboard()
        }
    }
    homeBtn.alpha = 1
    restartBtn.alpha = 1
    rewardAdBtn.alpha = 1
    gameCenterBtn.alpha = 1
}

restartBtn旁边的每个按钮都可以正确跟随字母更改。 restartBtn不受任何字母更改的影响。

我在代码中的其他任何地方都没有引用此按钮。

0 个答案:

没有答案