在 SpriteNode 上重复操作

Repeat action on a SpriteNode

我想在用户触摸 deckOfCards 时重复显示两张游戏卡中的一张。

到目前为止我让它工作了一次,但是当我再次点击 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])")
        }
    }
}

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])")
        }
    }
}