gpt4 book ai didi

ios - 在 SpriteNode 上重复操作

转载 作者:行者123 更新时间:2023-11-28 15:19:42 25 4
gpt4 key购买 nike

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

关于ios - 在 SpriteNode 上重复操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46238437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com