游戏持续时间越长,我怎样才能让我的炸弹落得更快或产生得更快?

How can I make my bombs either fall faster or spawn faster the longer the game lasts?

游戏持续时间越长,我怎样才能让我的炸弹落得更快或产生得更快?你活得越久,我的游戏就越难。我希望它每 10 个炸弹变得更难。我已经研究了好几天,并尝试了类似问题的多种答案。我相信我制造炸弹的方式使我更难做到这一点。提前致谢!

var bombSpawnSpeed : NSTimeInterval = 0.40
var bombCounter : Int = 0

这是我的更新功能。我的理解是这只是整个场景的定期更新功能。

  override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

  if bombCounter >= 10 {
        if bombSpawnSpeed > 0.15 {
            bombSpawnSpeed -= 0.05
            bombCounter = 0

        }
        else {
            bombSpawnSpeed = 0.1

        }

    }

这是炸弹

func enemies() {

    let bomb1 = SKSpriteNode(imageNamed: "AtomicBomb")
    let bomb2 = SKSpriteNode(imageNamed: "EMP")
    let bomb3 = SKSpriteNode(imageNamed: "BioBomb")
    let bomb4 = SKSpriteNode(imageNamed: "ChemBomb")

    let enemy = [bomb1, bomb2, bomb3, bomb4]


    // Enemy Physics
    for bomb in enemy {
        bomb1.size = CGSize(width: 55, height: 55)
        bomb2.size = CGSize(width: 55, height: 70)
        bomb3.size = CGSize(width: 30, height: 30)
        bomb4.size = CGSize(width: 45, height: 70)


        bomb.physicsBody = SKPhysicsBody(circleOfRadius: bomb.size.width / 4)
        bomb.physicsBody?.categoryBitMask = PhysicsCategory.enemy
        bomb.physicsBody?.collisionBitMask = PhysicsCategory.missile | PhysicsCategory.airDefense
        bomb.physicsBody?.contactTestBitMask = PhysicsCategory.missile | PhysicsCategory.airDefense
        bomb.physicsBody?.affectedByGravity = false 
        bomb.runAction(SKAction.speedBy(10, duration: 30))
        bomb.physicsBody?.dynamic = true
        bomb1.name = "enemy1"
        bomb2.name = "enemy2"
        bomb3.name = "enemy3"
        bomb4.name = "enemy4"

        print("bomb")

    }

    let deployAtRandomPosition = arc4random() % 4

    switch deployAtRandomPosition {
    case 0:
        bomb1.position.y = frame.size.height

        let PositionX = arc4random_uniform(UInt32(frame.size.width))

        bomb1.position.x = CGFloat(PositionX)

        self.addChild(bomb1)

        break

    case 1:
        bomb2.position.y = frame.size.height

        let PositionX = arc4random_uniform(UInt32(frame.size.width))

        bomb2.position.x = CGFloat(PositionX)

        self.addChild(bomb2)

        break

    case 2:
        bomb3.position.y = frame.size.height

        let PositionX = arc4random_uniform(UInt32(frame.size.width))

        bomb3.position.x = CGFloat(PositionX)

        self.addChild(bomb3)

        break

    case 3:
        bomb4.position.y = frame.size.height

        let PositionX = arc4random_uniform(UInt32(frame.size.width))

        bomb4.position.x = CGFloat(PositionX)

        self.addChild(bomb4)

        break

    default:

        break

    }

   bomb1.runAction(SKAction.moveTo(airDefense.position, duration: 5))
   bomb2.runAction(SKAction.moveTo(airDefense.position, duration: 5))
   bomb3.runAction(SKAction.moveTo(airDefense.position, duration: 5))
   bomb4.runAction(SKAction.moveTo(airDefense.position, duration: 5))

}

这就是我所说的 touches 开始制造炸弹

enemyTimer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("enemies"), userInfo: nil, repeats: true)

我认为更好的方法是

  1. 添加一个名为 lastTimeAddedBomb

  2. 的新变量
  3. 通过跟踪你释放了多少炸弹来决定你的间隔

  4. 检查您是否需要在 update 方法中添加新炸弹

像这样:

var lastTimeAddedBomb:NSDate
var numberOfBombsDropped:Int = 0
var difficultyLevel:Float = 1

override func update(currentTime: CFTimeInterval) {

    if (numberOfBombsDropped % 10 == 0) {
        difficultyLevel-= 0.1;
    }

    let intervalForBombs = difficultyLevel

    let timeFromLastBomb = NSDate().timeIntervalSinceDate(lastTimeAddedBomb)
    if (timeFromLastBomb >= intervalForBombs) {
        //call bomb
        //save last bomb date as now
        numberOfBombsDropped++
        lastTimeAddedBomb = NSDate()
    } 
}

您的代码不会增加 bombCounter,它会将 bombSpawnSpeed 重置为 0.1 9 out of 10 updates。 更新方法可能会经常降低 bombSpawnSpeed。但如果利率好,你可以使用它。 如果您不在其他地方增加 bombCounter,您可以在更新方法中增加它。 知道你上次投下炸弹的时间的变量也很好。

var lastDroppedBombTime: NSDate

override func update(currentTime: CFTimeInterval) {
    bombCounter += 1
    if bombCounter >= 10 {
        bombSpawnSpeed -= 0.05
        bombCounter = 0
    }

    let differenceInTime = NSDate().timeIntervalSinceDate(lastDroppedBombTime) // or use the difference of the parameter currentTime and lastDroppedBombTime
    if (differenceInTime >= bombSpawnSpeed) {
        enemies() // spawn the new bomb(s)
        lastDroppedBombTime = NSDate()
    }
}