freeze - Java Game Keeps Freezing When I Start A Thread? -
i'm making above mode 2d zombie shooter, , i'm having problems thread. here's deal. had character shoot bullet every time pressed space. problem if held space, shoot one, pause, , shoot lot of bullets. there bunch of ways prepare this, want way because leaves room future changes shoot speed. here code thread causing problems:
package threads; import game.gamecore; public class shoot extends gamecore implements runnable { /** * when start thread, entire game freezes, , not know * why... need fix. not prepare problem take out "shoot" * or "sleep"... */ public void run() { while (shooting && gamerunning) { // shooting made true when space // pressed, , set false when space // released. gamerunning true // if game running, // is. removing either of these // doesnt work either. player.shoot(); // player comes gamecore class, , // represents player entity. if remove this, // nil changes. seek { thread.sleep(bulletshootspeedmillis); // bulletshootspeedmillis // equal 1000, // makes no difference // alter } grab (interruptedexception e) { e.printstacktrace(); } } } }
so here dang problem. comments kinda point them out, sick list them. if remove obvious things, such player.shoot();
or thread.sleep(bulletshootspeedmillis);
or 1 of things in while loop, nil changes. issue when initiallize thread, with
else if (key == integer.parseint(commands[6])) { shooting = true; new thread(new shoot()).run(); }
the entire game freezes. nil happens @ all. moment start thread space, game freezes, , cannot figure out why!!! previous version with:
else if (key == integer.parseint(commands[6])) { player.shoot(); }
it works fine. please help! in advance! :)
edit: quick answer. needless say, major learning experience simple mistakes xd
ayyyy!
new thread(new shoot()).run(); // ***** !!!!
you don't start thread calling run()
method, since phone call code in same thread calling code. start new thread calling start()
method.
java freeze if-statement
No comments:
Post a Comment