gpt4 book ai didi

Swift 3 子弹发射延迟

转载 作者:搜寻专家 更新时间:2023-10-31 22:58:08 25 4
gpt4 key购买 nike

在我的游戏中,您点击屏幕上的任意位置,子弹就会朝那个方向飞去。唯一的问题是您可以尽可能快地拍摄。有什么办法可以在每次拍摄后增加延迟。所以我想开枪,等1秒再开枪。这是我在 touchesEnded 中的代码:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

guard let touch = touches.first else {
return
}
let touchLocation = touch.location(in: self)

//Set up initial location of bullet and properties
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.name = "Bullet"
bullet.position = player.position
bullet.setScale(0.75)
bullet.zPosition = 1
bullet.physicsBody = SKPhysicsBody(circleOfRadius: bullet.size.width/2)
bullet.physicsBody?.isDynamic = true
bullet.physicsBody?.categoryBitMask = PhysicsCategory.Projectile
bullet.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
bullet.physicsBody?.collisionBitMask = PhysicsCategory.None
bullet.physicsBody?.usesPreciseCollisionDetection = true

//Determine offset of location to bullet
let offset = touchLocation - bullet.position

//Stops Bullet from shooting backwards
if (offset.y < 0) { return }

addChild(bullet)

//Get the direction of where to shoot
let direction = offset.normalized()

//Make it shoot far enough to be guaranteed off screen
let shootAmount = direction * 1000

//Add the shoot amount to the current position
let realDest = shootAmount + bullet.position

//Create the actions

if currentGameState == gameState.inGame {
let actionMove = SKAction.move(to: realDest, duration: 1.0)
let actionMoveDone = SKAction.removeFromParent()
bullet.run(SKAction.sequence([actionMove, actionMoveDone]))
}

}

感谢您的帮助。

最佳答案

这是一种更简单的方法,基于日期的使用:

var time = Date()

func shoot(after timeInterval: Double) {
guard Date() - timeInterval > time else {
print("WAIT")
return
}
print("SHOOT")
time = Date() // reset the timer
}

// CALL THIS INSIDE touchesEnded
shoot(after: 1)

只需根据您的需要进行修改:]

关于Swift 3 子弹发射延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838138/

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