2017-09-15 53 views
0

我想重複顯示兩張遊戲卡中的一張,每當用戶觸摸deckOfCards在SpriteNode上重複動作

到目前爲止,我得到了它的工作,但是當我再次點擊deckOfCards時,卡片不會改變。試着用10個或更多的卡片名稱,也沒有工作。

class GameScene: SKScene { 

let cardname = ["card2", "ace"] 
let randomNumber = Int(arc4random_uniform(13)) 
var deckOfCards = SKSpriteNode() 
var yourCard = SKSpriteNode() 

override func didMove(to view: SKView) { 
    deckOfCards = self.childNode(withName: "deckOfCards") as! SKSpriteNode 
    yourCard = self.childNode(withName: "yourCard") as! SKSpriteNode 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    self.view?.endEditing(true) 

    for touch: AnyObject in touches { 

     let location = touch.location(in: self) 
     let node : SKNode = self.atPoint(location) 
     if node.name == "deckOfCards" { 
      yourCard.texture = SKTexture(imageNamed: "\(cardname[randomNumber])") 
     } 
    } 
} 

回答

1

randomNumber是touchesBegan以外的常數。 它永遠不會改變。把它放進touchesBegan

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    self.view?.endEditing(true) 

    for touch: AnyObject in touches { 
     let location = touch.location(in: self) 
     let node = self.atPoint(location) 
     let randomNumber = Int(arc4random_uniform(13)) 

     if node.name == "deckOfCards" { 
      yourCard.texture = SKTexture(imageNamed: "\(cardname[randomNumber])") 
     } 
    } 
} 
+0

快速回答,點上並解決問題。非常感謝 – junxi