【问题标题】:Repeat action on a SpriteNode在 SpriteNode 上重复操作
【发布时间】:2017-09-15 11:24:47
【问题描述】:

每当用户触摸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])")
        }
    }
}

【问题讨论】:

    标签: ios swift swift3 skspritenode touchesbegan


    【解决方案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])")
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-06
      • 2011-12-29
      • 2017-01-08
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多