Swift 4

时间:2018-03-18 11:17:17

标签: ios swift sprite-kit

我试图在触摸屏幕的同时连续发射子弹。这是我到目前为止所得到的,但它并没有真正发挥作用。

func fireBullet() {

    let bullet = SKSpriteNode(imageNamed: "bullet")
    bullet.name = "Bullet"
    bullet.setScale(1.5) // Bullet Size
    bullet.position = player.position
    bullet.zPosition = 1
    bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.size)
    bullet.physicsBody!.affectedByGravity = false
    bullet.physicsBody!.categoryBitMask = PhysicsCategories.Bullet
    bullet.physicsBody!.collisionBitMask = PhysicsCategories.None
    bullet.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy
    self.addChild(bullet)

    let moveBullet = SKAction.moveTo(y: self.size.height + bullet.size.height, duration: 1)
    let deleteBullet = SKAction.removeFromParent()

    let bulletSequence = SKAction.sequence([bulletSound, moveBullet, deleteBullet])
    let bulletRepeat = SKAction.repeatForever(bulletSequence)
    bullet.run(bulletRepeat)
}

2 个答案:

答案 0 :(得分:0)

您可以使用在touchesBegan中启动的Timer对象,并在touchesEnded方法中取消它。

对于计时器,我使用下面的课程:

class MyTimerHandlerObject: NSObject {

var timerCounter: CGFloat = 0
var myTimer = Timer()

override init() {super.init()}

@objc func myTimerHandler(_ theTimer: Timer) {

    timerCounter += 0.1

    // Do what needs do be done here ... 

}

func startMyTimer() {
    timerCounter = 0
    self.myTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(MyTimerHandlerObject.myTimerHandler(_:)), userInfo: nil, repeats: true)
}

func stopMyTimer() {
    self.myTimer.invalidate()
    timerCounter = 0
}

}

然后,您可以通过声明MyTimerHandlerObject类型的变量并使用方法' StartMyTimer'来轻松启动计时器并将其停止。和' StopMyTimer'

答案 1 :(得分:0)

当您向下按屏幕并在抬起手指时将其关闭时,您需要的是打开火焰。您可以使用触摸开始将变量设置为true表示您应该触发isFiring = true然后在更新中捕获该变量。当您抬起手指时,触摸结束将关闭变量。

另一个不错的选择是你可以实时调整firingInterval变量来调整火力。

private var updateTime: Double = 0
private var isFiring = false
//rate of fire, 1 means fire once a second
private var firingInterval: Double = 1

override func update(_ currentTime: TimeInterval) {

    //check if we should be firing if not get outta here
    guard isFiring else { return }

    if updateTime == 0 {
        updateTime = currentTime
    }

    if currentTime - updateTime > firingInterval {
        self.fireBullet()
        updateTime = currentTime
    }
}

override touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    isFiring = true
}

override touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    isFiring = false
}

func fireBullet() {

    let bullet = SKSpriteNode(imageNamed: "bullet")
    bullet.name = "Bullet"
    bullet.setScale(1.5) // Bullet Size
    bullet.position = player.position
    bullet.zPosition = 1
    bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.size)
    bullet.physicsBody!.affectedByGravity = false
    bullet.physicsBody!.categoryBitMask = PhysicsCategories.Bullet
    bullet.physicsBody!.collisionBitMask = PhysicsCategories.None
    bullet.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy
    self.addChild(bullet)

    let moveBullet = SKAction.moveTo(y: self.size.height + bullet.size.height, duration: 1)
    let deleteBullet = SKAction.removeFromParent()

    let bulletSequence = SKAction.sequence([bulletSound, moveBullet, deleteBullet])
    //let bulletRepeat = SKAction.repeatForever(bulletSequence)
    bullet.run(bulletSequence )
}