2017-11-25 111 views
0

我在不同的关卡创建了一款游戏。如果用户达到特定的gameScore,alertButton应该弹出(已经工作),但只要用户切换场景,按钮​​应该消失。用我的代码,按钮不会消失。我如何让图像只出现一次?如何添加转换后消失的图像?

这里是我的代码:

var alertButton = SKSpriteNode.init(imageNamed: "AlertButton") 
var totalGameScore = 0 

class Game: SKScene { 
override func didMove(to view: SKView) { 
if totalGameScore > 50 { //alert button appears and should disappear after scene changes 
self.addChild(alertButton) 
} 
if totalGameScore > 100 { //alert button appears again and should disappear after scene changes 
self.addChild(alertButton) 
    } 
} 
} 

回答

0

你的逻辑是,如果alertbutton实际上是要添加第二个按钮时totalGameScore> 100,因为boths IFS将执行出现一次totalGameScore> 50秒。

为了处理这种情况,你应该有两个标志:

var hasShown50ScoreButton = false 
var hasShow100ScoreButton = false 

那么你检查:

class Game: SKScene { 
    override func didMove(to view: SKView) { 
     if totalGameScore > 50 && hasShown50ScoreButton == false { 
     self.addChild(alertButton) 
     hasShown50ScoreButton = true 
     } else if totalGameScore > 100 && hasShown100ScoreButton == false { 
     self.addChild(alertButton) 
     hasShown100ScoreButton = true 
     } else if alertButton.superview != nil { 
     aleterButton.removeFromSuperview() 
    } 
}