在 Xcode Swift 中创建重复计时器的问题
Problems with creating a repeating timer in Xcode Swift
我目前正在为我的游戏编写一个重复计时器,每秒生成一枚硬币,但是,当我 运行 代码仅生成一枚硬币然后自行删除而不再跟随时,这是一个示例我正在使用的代码,我确信对此有一个简单的答案,但我已经坚持了一天。
@objc let coin = SKSpriteNode(imageNamed: "Coin")
var gametimer = Timer()
override func didMove(to view: SKView) {
gametimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(getter: coin), userInfo: nil, repeats: true)
coin.position = CGPoint(x: self.size.width/2, y: self.size.height)
coin.setScale(1)
coin.zPosition = 1
addChild(coin)
let moveAction = SKAction.moveTo(y: 0, duration: 0.9)
let deleteAction = SKAction.removeFromParent()
coin.run(SKAction.sequence([moveAction,deleteAction]))
说明我的评论的基本示例:
// Timer property
var gameTimer = Timer()
// In viewDidLoad, for example, set the timer only once
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(spawnCoins), userInfo: nil, repeats: true)
// Place the code that should repeat in this function
@objc func spawnCoins(){
print(“new coin spawned!”)
}
我目前正在为我的游戏编写一个重复计时器,每秒生成一枚硬币,但是,当我 运行 代码仅生成一枚硬币然后自行删除而不再跟随时,这是一个示例我正在使用的代码,我确信对此有一个简单的答案,但我已经坚持了一天。
@objc let coin = SKSpriteNode(imageNamed: "Coin")
var gametimer = Timer()
override func didMove(to view: SKView) {
gametimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(getter: coin), userInfo: nil, repeats: true)
coin.position = CGPoint(x: self.size.width/2, y: self.size.height)
coin.setScale(1)
coin.zPosition = 1
addChild(coin)
let moveAction = SKAction.moveTo(y: 0, duration: 0.9)
let deleteAction = SKAction.removeFromParent()
coin.run(SKAction.sequence([moveAction,deleteAction]))
说明我的评论的基本示例:
// Timer property
var gameTimer = Timer()
// In viewDidLoad, for example, set the timer only once
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(spawnCoins), userInfo: nil, repeats: true)
// Place the code that should repeat in this function
@objc func spawnCoins(){
print(“new coin spawned!”)
}