Thursday, 15 January 2015

java - How to spawn enemies periodically -



java - How to spawn enemies periodically -

i attempting create game enemies spawn randomly , move towards point on screen. problem programme waits them spawn , them starts moving them. want them start moving spawn , have short break in between each one.

here's 2 main methods behind making them move:

public void newlevel() { runonuithread(new runnable(){ public void run() { int = 0; while( < 10){ addspeks(); i++; seek { thread.sleep(2000); } catch(interruptedexception ie){ ie.printstacktrace(); } } } }); } public void addspeks() { spek newspek = new spek(this); setrandomregion(); newspek.setx(spawnx); newspek.sety(spawny); relativelayout.addview(newspek); speklist.add(newspek); newspek.setonclicklistener(speklistener); newspek.animate().x(finalx).y(finaly).setduration(8000); }

spek simple class extends imageview , setrandomregion() selects random x, y coordinate on border of screen. have counter in while loop simplicity purposes move conditional infinite loop stops if lose game.

thanks help , allow me know if problem needs more clarification.

you have re-run thread when finished, in android can done this:

first, create runnable:

private runnable spawnenemies = new runnable(){ public void run(){ //your code here... spawnhandler.postdelayed(2000, this); } };

then, create handler, class runs thread (or, well, runnable) , start runnable this:

handler spawnhandler = new handler(); spawnhandler.post(new spawnenemies());

this run every 2 seconds, add together boolean or simmilar stop loop manually.

java android spawning

No comments:

Post a Comment