2017-09-15 54 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