2017-11-25 109 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() 
    } 
}