Author Topic: Spawning enemies  (Read 1983 times)

caseymac

  • Active Member
  • ***
  • Posts: 101
  • Karma: +0/-0
    • View Profile
Spawning enemies
« on: Mon, Oct 24, 2011 »
my game has waves in it, and im wondering, how do i spawn enemies every 5 seconds or so. would i use flxtimer to count down and spawn one once 5 seconds has passed?

caseymac

  • Active Member
  • ***
  • Posts: 101
  • Karma: +0/-0
    • View Profile
Re: Spawning enemies
« Reply #1 on: Mon, Oct 24, 2011 »
this is the code im using so far..
Code: [Select]
timer += FlxG.elapsed
if (timer > 5) {
timer = 0
add(_enemies)
}


FlxG.log(timer)
but it doesn't work and logs the timer as a not available number.

KunoNoOni

  • "Never tell me the odds"
  • Active Member
  • ***
  • Posts: 170
  • Karma: +0/-0
  • Code is Love
    • View Profile
Re: Spawning enemies
« Reply #2 on: Mon, Oct 24, 2011 »
Did you put this code in the update function? Also you can check here for additional help with timers.


-KunoNoOni

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Spawning enemies
« Reply #3 on: Mon, Oct 24, 2011 »
this is the code im using so far..
Code: [Select]
timer += FlxG.elapsed
if (timer > 5) {
timer = 0
add(_enemies)
}


FlxG.log(timer)
but it doesn't work and logs the timer as a not available number.

That code doesn't work because Flxg.elapsed is not reseting itself, so you should have a timer to detect last time you unleashed a horde/wave and compare it with current FlxG.elapsed and if the difference is bigger than a value, then release another variable. But as KunoNoOni says use proper timers.
blog, twitter, Check out my award winning game, Rot Gut:

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Spawning enemies
« Reply #4 on: Sun, Oct 30, 2011 »
is your timer in Number. If your timer in Integer change it to Number

camasthecat

  • Contributor
  • ****
  • Posts: 461
  • Karma: +0/-0
  • WOW! 400+ Posts!? Jeez! Do I have a life!?!?
    • View Profile
    • My site for thermalJS, an HTML5/Javascript engine kinda-like flixel:
Re: Spawning enemies
« Reply #5 on: Sun, Oct 30, 2011 »
Is a Number basically a "whole number"?

Alextfish

  • Active Member
  • ***
  • Posts: 174
  • Karma: +0/-0
    • View Profile
Re: Spawning enemies
« Reply #6 on: Mon, Oct 31, 2011 »
No, a Number is a floating point number, something like single precision. Integer or uint are "whole numbers" with no fractional part.

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Spawning enemies
« Reply #7 on: Mon, Oct 31, 2011 »
That code doesn't work because Flxg.elapsed is not reseting itself, so you should have a timer to detect last time you unleashed a horde/wave and compare it with current FlxG.elapsed and if the difference is bigger than a value, then release another variable. But as KunoNoOni says use proper timers.

Nope. FlxG.elapsed is automatically set by the flixel engine and measures the time since the last frame (don't modify it unless you want all other sorts of weirdness happening). So for a 30fps game, FlxG.elapsed should always be 0.0333333 (unless a game is lagging and the frame rate changes). Because the frame rate is typically constant, it may look like elapsed is not getting reset - but it is. So you need to create a new variable, and add elapsed to it each frame to get the total number of time elapsed, which is exactly what he's doing.

OP, you mentioned FlxTimer.... is timer by chance an FlxTimer? It should be a number as others have suggested.
« Last Edit: Mon, Oct 31, 2011 by wg/funstorm »

camasthecat

  • Contributor
  • ****
  • Posts: 461
  • Karma: +0/-0
  • WOW! 400+ Posts!? Jeez! Do I have a life!?!?
    • View Profile
    • My site for thermalJS, an HTML5/Javascript engine kinda-like flixel:
Re: Spawning enemies
« Reply #8 on: Mon, Oct 31, 2011 »
I meant "whole number" as a floating point. Thanks.

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Spawning enemies
« Reply #9 on: Tue, Nov 1, 2011 »
Oh okay. The definition of whole number is no floating point / decimals  ;)

http://dictionary.reference.com/browse/whole+number