Reputation: 570
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
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))
}
This is what I am calling in touches Began to create the bombs
enemyTimer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("enemies"), userInfo: nil, repeats: true)
Upvotes: 1
Views: 104
Reputation: 12446
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()
}
}
Upvotes: 3
Reputation: 2897
I think a better approach is
Add a new var called lastTimeAddedBomb
Decide on your interval by tracking how much bombs you released
update
methodLike 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()
}
}
Upvotes: 3