2017-04-11 55 views
0

我在創建可變速度的射擊槍(即左輪手槍vs機槍)時遇到了麻煩。現在我有子彈的射擊實際驗證碼:xcode 8 swift 3減速功能的「開火」時間

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速率相同)。謝謝你的幫助!

回答

0

一個簡單的方法是添加一個冷靜降低你的子彈產卵率。一個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 }