timer - XNA Simple way to spawn enemys -
im trying spawn enemies list. i've tryed multiple ways in doing so. can't work. there simple way spawn enemes every 3 seconds? edit
i tried spawning this: problem: spawns once
protected void adjustspawntimes(gametime gametime) { // if spawn maximum time > 500 milliseconds, // decrease spawn time if it's time // based on spawn-timer variables if (enemyspawnmaxmilliseconds > 500) { timesincelastspawntimechange += gametime.elapsedgametime.milliseconds; if (timesincelastspawntimechange > nextspawntimechange) { timesincelastspawntimechange -= nextspawntimechange; if (enemyspawnmaxmilliseconds > 1000) { enemyspawnmaxmilliseconds -= 100; enemyspawnminmilliseconds -= 100; } else { enemyspawnmaxmilliseconds -= 10; enemyspawnminmilliseconds -= 10; } } } }
and this: problem: again spawns once
private void spawnenemy() { vector2 speed = vector2.zero; vector2 position = vector2.zero; // default frame size point framesize = new point(75, 75); int screenwidth = graphicsdevice.presentationparameters.backbufferwidth; int screenheight = graphicsdevice.presentationparameters.backbufferheight; // randomization: // - randomly choose side of screen place enemy // - randomly create position along side of screen // - randomly choose speed enemy switch (rand.next(4)) { case 0: // left right position = new vector2(-framesize.x, (rand.next(0, screenheight - framesize.y))); speed = new vector2(rand.next(needleminv, needlemaxv), 0); break; case 1: // right left position = new vector2(screenwidth, (rand.next(0, screenheight - framesize.y))); speed = -new vector2(rand.next(needleminv, needlemaxv), 0); break; case 2: // bottom top position = new vector2(rand.next(0, screenwidth - framesize.x), screenheight); speed = -new vector2(0, (rand.next(needleminv, needlemaxv))); break; case 3: // top bottom position = new vector2(rand.next(0, screenwidth - framesize.x), -framesize.y); speed = new vector2(0, rand.next(needleminv, needlemaxv)); break; }
and 1 spawns not move ive played around updat , draw method nothing seems work
list<enemy> needlelist = new list<enemy>(); texture2d needle; float spawntime = 10; const float timer = 10; bool spawnn = true;
in update:
timer = (float)gametime.totalgametime.totalseconds; spawntime -= (float)gametime.elapsedgametime.totalseconds; if (timer < 0) { foreach (enemy needele in needlelist) { spawnn = !spawnn; needele.update(gametime); spawntime = timer; }
in draw:
if (spawnn) { foreach (enemy needele in needlelist) { needele.draw(gametime, spritebatch); } }
you need break problem steps:
you need global variables represent timer , represent amount of time wait e.g.
float spawntimer; const float time_to_wait = 3.0f; // don't make const if need change.
next in update need increment time has passed since last update. seem using multiple timers , making more awkward need be. should need multiple timers if you're needing time multiple things.
spawntimer += gametime.elapsedgametime.totalseconds;
next, when time passed greater amount of time time required wait. spawn enemy , reset timer zero.
if want spawn more 1 enemy @ time, add loop it's condition being loop while iterator less number of enemies spawn.
i've delibrately not given last 2 areas, steps outlined should relatively simple. rest of code should work once done.
if doesn't work, debug code , check if spawnenemy called or not.
Comments
Post a Comment