2017-04-12 41 views
0

我試圖讓用戶使用硬幣購買節點,然後在遊戲中使用此節點。檢查UserDefaults的值並創建一條IF語句

我試圖設置一些代碼,檢查節點是否已被觸摸/購買之前,如果沒有,顯示灰色的節點圖像。

此外,如果用戶的硬幣數量大於一個整數,則在該節點上觸摸啓用允許他們購買它。

這就是我想我可以用它來看看該節點是否已訂購/前感動,如果沒有,給碰的能力/購買,如果他們有足夠的硬幣:

if UserDefaults.standard.bool(forKey: "ship2") == true{ 
     let ship2Texture = SKTexture(imageNamed: "ship2.png") 
     ship2 = SKSpriteNode(texture: ship2Texture) 
     ship2.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY) 
     self.addChild(ship2) 
    }else{ 
     coinImage1 = SKSpriteNode(texture: coinImageTexture) 
     coinImage1.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY + 105) 
     coinImage1.zPosition = 1 
     self.addChild(coinImage1) 

     coinLabel1.fontName = "MarkerFelt-Thin" 
     coinLabel1.fontSize = 25 
     coinLabel1.fontColor = .black 
     coinLabel1.text = "20" 
     coinLabel1.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY + 105) 
     coinLabel1.zPosition = 1.1 
     self.addChild(coinLabel1) 

     let greyship2Texture = SKTexture(imageNamed: "greyship2.png") 
     greyship2 = SKSpriteNode(texture: greyship2Texture) 
     greyship2.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY) 

     self.addChild(greyship2) 
    } 

現在這裏是我在我的觸動是什麼開始:

if(atPoint(location) == greyship2) { 
     if coinScore > 3 { 
      let oldValue = UserDefaults.standard.integer(forKey: "COINSCORE") 
      let newValue = oldValue - 20 
      UserDefaults.standard.set(newValue, forKey: "COINSCORE") 
      UserDefaults.standard.set(true, forKey: "ship2") 
     } else { 

     } 
    } 
    if(atPoint(location) == ship2){ 
     if let scene = GameScene2(fileNamed: "GameScene2") { 
      // Set the scale mode to scale to fit the window 
      scene.scaleMode = .aspectFill 

      // Present the scene 
      view!.presentScene(scene, transition: SKTransition.doorway(withDuration: 1.2));  
      } 
    } 

唯一的問題是,當用戶有足夠的硬幣和接觸greyship2它不會再檢查的初始代碼。我必須重新啓動coinScore的應用程序,以減少20,並使彩色船舶實際顯示。

我試過把第一部分代碼放在更新函數中,但它崩潰了。

回答

1

它看起來好像在購買後沒有更新UI。您正在更改UserDefaults值,因此下次運行時將顯示正確的精靈。但是你需要做的是在購買之後更新UI,因爲你的代碼根據你的UserDefaults值選擇了精靈而不是再次運行。如果您的第一個代碼位於某個功能中,那麼您可以在購買完成後調用該功能。

此外,一旦您更改了值以便立即將其寫入磁盤,最好調用UserDefaults.standard.synchronize()

試試這個

if(greyship2.contains(location)) { 
    if coinScore > 3 { 
     let oldValue = UserDefaults.standard.integer(forKey: "COINSCORE") 
     let newValue = oldValue - 20 

     UserDefaults.standard.set(newValue, forKey: "COINSCORE") 
     UserDefaults.standard.set(true, forKey: "ship2") 
     UserDefaults.standard.synchronize() 

     //Now it has been brought update the UI and other necessary things 

     //If the code is in a function call it 

     //updateUI() 

     //else change the sprites 

     //Remove the grey ship 
     greyship2.setScale(0) 
     greyship2.removeFromParent() 

     //create the new ship and add it to the scene 
     let ship2Texture = SKTexture(imageNamed: "ship2.png") 
     ship2 = SKSpriteNode(texture: ship2Texture) 
     ship2.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY) 
     ship2.setScale(1) 
     self.addChild(ship2) 
     break //finish the touchesbegan call 
    } 
} 

if(ship2.contains(location)) { 
     if let scene = GameScene2(fileNamed: "GameScene2") { 
      // Set the scale mode to scale to fit the window 
      scene.scaleMode = .aspectFill 

      // Present the scene 
      view!.presentScene(scene, transition: SKTransition.doorway(withDuration: 1.2));  
     } 
} 

希望這有助於

+0

的唯一理由罵同步明確這裏是因爲它是自動保存之前的應用程序可能會崩潰。如果應用程序有效,這不是必需的。 – rmaddy

+0

這很好。唯一的問題是,當coinScore> 3和我碰到greyShip時。它也觸發了接觸ship2的代碼。因此,而不是船2出現,然後在船2上敲擊改變場景,它一觸即發呢?希望這是有道理 –

+0

我想我明白嘗試將灰船的規模設置爲0,一旦它被帶來。 –