如何添加转换后消失的图像?

时间:2017-11-25 11:42:05

标签: ios swift sprite-kit

我在不同级别创建了一款游戏。如果用户到达某个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)
  }
 }
}  

1 个答案:

答案 0 :(得分:0)

您的逻辑是,一旦totalGameScore>出现警告按钮; 50.当totalGameScore>时,第二个if实际上将添加第二个按钮。 100因为两个ifs都会执行。

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

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()
     }
} 
相关问题