xcode 8 swift 3 减慢函数的 "firing" 时间
xcode 8 swift 3 slow down function's "firing" time
我在制作以可变速度射击的枪时遇到了问题(即左轮手枪与机关枪)。现在我有实际发射子弹的代码:
class GameScene: SKScene, SKPhysicsContactDelegate {
func spawnBullets(_ location: CGPoint){
let Bullet = SKSpriteNode(imageNamed: "circle")
Bullet.zPosition = -1
Bullet.position = CGPoint(x: ship.position.x,y: ship.position.y)
Bullet.size = CGSize(width: 30, height: 30)
Bullet.physicsBody = SKPhysicsBody(circleOfRadius: 15)
Bullet.physicsBody?.categoryBitMask = PhysicsCategory.Bullet
Bullet.physicsBody?.collisionBitMask = 0
Bullet.name = "Bullet"
Bullet.physicsBody?.isDynamic = true
Bullet.physicsBody?.affectedByGravity = false
self.addChild(Bullet)
var dx = CGFloat(location.x - base2.position.x)
var dy = CGFloat(location.y - base2.position.y)
let magnitude = sqrt(dx * dx + dy * dy)
dx /= magnitude
dy /= magnitude
let vector = CGVector(dx: 30.0 * dx, dy: 30.0 * dy)
Bullet.physicsBody?.applyImpulse(vector)
然后我在后面的部分有这个
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches ){
let location = touch.location(in: self)
if ball3.frame.contains(location) && base2.frame.contains(location) == false{
spawnBullets(location)}
我试过使用定时器功能,但它似乎只会让事情变得更糟。现在子弹以每秒 60 发的速度发射(或与 fps 相同的速度)。感谢您的帮助!
一个简单的方法是增加冷却时间以降低子弹产生率。添加 cooldown
属性 到您的场景:
var cooldown = 0
当调用 spawnBullets
时,您可以根据枪支设置两次射击之间所需的冷却时间延迟,例如cooldown = 30 // Basing this on a constant 60fps the bullet will have a cooldown of 0.5 seconds
您需要为 cooldown
添加检查,在 touchesMoved
中,您可以将 && cooldown == 0
添加到条件中。
在您的 update
方法中,递减 cooldown
:
if cooldown > 0 { cooldown -= 1 }
我在制作以可变速度射击的枪时遇到了问题(即左轮手枪与机关枪)。现在我有实际发射子弹的代码:
class GameScene: SKScene, SKPhysicsContactDelegate {
func spawnBullets(_ location: CGPoint){
let Bullet = SKSpriteNode(imageNamed: "circle")
Bullet.zPosition = -1
Bullet.position = CGPoint(x: ship.position.x,y: ship.position.y)
Bullet.size = CGSize(width: 30, height: 30)
Bullet.physicsBody = SKPhysicsBody(circleOfRadius: 15)
Bullet.physicsBody?.categoryBitMask = PhysicsCategory.Bullet
Bullet.physicsBody?.collisionBitMask = 0
Bullet.name = "Bullet"
Bullet.physicsBody?.isDynamic = true
Bullet.physicsBody?.affectedByGravity = false
self.addChild(Bullet)
var dx = CGFloat(location.x - base2.position.x)
var dy = CGFloat(location.y - base2.position.y)
let magnitude = sqrt(dx * dx + dy * dy)
dx /= magnitude
dy /= magnitude
let vector = CGVector(dx: 30.0 * dx, dy: 30.0 * dy)
Bullet.physicsBody?.applyImpulse(vector)
然后我在后面的部分有这个
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches ){
let location = touch.location(in: self)
if ball3.frame.contains(location) && base2.frame.contains(location) == false{
spawnBullets(location)}
我试过使用定时器功能,但它似乎只会让事情变得更糟。现在子弹以每秒 60 发的速度发射(或与 fps 相同的速度)。感谢您的帮助!
一个简单的方法是增加冷却时间以降低子弹产生率。添加 cooldown
属性 到您的场景:
var cooldown = 0
当调用 spawnBullets
时,您可以根据枪支设置两次射击之间所需的冷却时间延迟,例如cooldown = 30 // Basing this on a constant 60fps the bullet will have a cooldown of 0.5 seconds
您需要为 cooldown
添加检查,在 touchesMoved
中,您可以将 && cooldown == 0
添加到条件中。
在您的 update
方法中,递减 cooldown
:
if cooldown > 0 { cooldown -= 1 }