Timmy Sorensen February 2016

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

How can I make my bombs either fall faster or spawn faster the longer the game lasts? I am trying to make my game harder the longer you stay alive. I would like it to get harder every 10 bombs. I have been researching this for days and have tried multiple answers to similar questions. I believe the way I had to make my bombs is what is making it harder for me to do this. Thanks in advance!

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

this is in my update func. My understanding is this is just the regular update func of the whole scene.

  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

        }

    }

Here are the bombs

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
        

Answers


Eli Braginskiy February 2016

I think a better approach is

  1. Add a new var called lastTimeAddedBomb

  2. Decide on your interval by tracking how much bombs you released

  3. Check if you need to add a new bomb in your update method

Like this:

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()
    } 
}


Godric February 2016

Your code does not increment the bombCounter and it does reset the bombSpawnSpeed to 0.1 9 out of 10 updates. The update method may be to often to reduce the bombSpawnSpeed. But if the rate is good you could use it. If you do not increase the bombCounter elsewhere you could increase it in the update method. A variable that knows the last time you dropped a bomb would be good too.

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()
    }
}

Post Status

Asked in February 2016
Viewed 1,121 times
Voted 14
Answered 2 times

Search




Leave an answer