2017-11-25 32 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

あなたのロジックは、実際にtotalGameScore> 100がbothsのIFSが実行されますので、ときに、第2のボタンを追加しようとしている場合alertbuttonはtotalGameScore> 50秒後に表示されていることである:

は、ここに私のコードです。そして、あなたがチェックし

var hasShown50ScoreButton = false 
var hasShow100ScoreButton = false 

次の2つのフラグを持っている必要があり、このような状況に対処するには

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() 
    } 
} 
関連する問題