java - Why won't my rocket lives decrease upon collision? -
ok, in game design class using greenfoot create game. asked make hardest asteroid game while making fair. decided add set amount of rocket lives. got set , works, never decrements rocket lives, how do it?
here code:
import greenfoot.*; // (world, actor, greenfootimage, , greenfoot) import java.util.list; /** * rocket can controlled arrowkeys: up, left, right. * gun fired hitting 'space' key. * * @author poul henriksen * @author michael kölling * * @version 2.0 */ public class rocket extends mover { private int gunreloadtime; // minimum delay between firing gun. private int reloaddelaycount; // how long ago fired gun last time. private vector acceleration; // vector used accelerate when using booster. private vector deacceleration; private int shotsfired; // number of shots fired. private int rocketlives; private int collisiondamage = 999; private greenfootimage rocket = new greenfootimage("rocket.png"); private greenfootimage rocketwiththrust = new greenfootimage("rocketwiththrust.png"); /** * initialise rocket. */ public rocket() { gunreloadtime = 10; reloaddelaycount = 0; acceleration = new vector(0, 0.035); // used accelerate when thrust on deacceleration = new vector(0, -0.035); increasespeed(new vector(13, 0.3)); // drifting shotsfired = 0; rocketlives = 3; } /** * rocket's gotta do. (which is: flying about, , turning, * accelerating , shooting when right keys pressed.) */ public void act() { if (rocketlives == 0) { getworld().removeobjects(getworld().getobjects(null)); greenfoot.stop(); } else { move(); checkkeys(); checkcollision(); reloaddelaycount++; } } /** * return number of shots fired rocket. */ public int getshotsfired() { return shotsfired; } /** * set time needed re-loading rocket's gun. shorter time is, * faster rocket can fire. (initial) standard time 20. */ public void setgunreloadtime(int reloadtime) { gunreloadtime = reloadtime; } /** * check whether colliding asteroid. */ private void checkcollision() { asteroid asteroid = (asteroid) getoneintersectingobject(asteroid.class); if (asteroid != null) { getworld().addobject(new explosion(), getx(), gety()); getworld().addobject(new rocket(), (1350/2)-20, 1000/2); if(istouching(asteroid.class)) { asteroid.hit(999); } getworld().removeobject(this); rocketlives--; } } /** * check whether there key pressed , react them. */ private void checkkeys() { ignite(greenfoot.iskeydown("w")); if(greenfoot.iskeydown("s")) { deignite(greenfoot.iskeydown("s")); } if(greenfoot.iskeydown("a")) { turn(-5); } if(greenfoot.iskeydown("d")) { turn(5); } if(greenfoot.iskeydown("space")) { fire(); } if(greenfoot.iskeydown("shift")) { int ran1 = (int)(math.random()*1350); int ran2 = (int)(math.random()*1000); getworld().addobject(new asteroid(), ran1, ran2); } } /** * should rocket ignited? */ private void ignite(boolean boosteron) { if (boosteron) { setimage(rocketwiththrust); acceleration.setdirection(getrotation()); increasespeed(acceleration); } else { setimage(rocket); } } private void deignite(boolean boosteron) { if (boosteron) { setimage(rocketwiththrust); deacceleration.setdirection(getrotation()); increasespeed(deacceleration); } else { setimage(rocket); } } /** * fire bullet if gun ready. */ private void fire() { if (reloaddelaycount >= gunreloadtime) { bullet b = new bullet(getmovement().copy(), getrotation()); getworld().addobject(b, getx(), gety()); b.move(); shotsfired++; reloaddelaycount = 0; // time since last shot fired } } }
i think saw error (debugging should show well):
it's following part in checkcollision()
:
getworld().addobject(new rocket(), (1350/2)-20, 1000/2); //here if(istouching(asteroid.class)) { asteroid.hit(999); } getworld().removeobject(this); //and here
on collision add new rocket world (which initialized 3 lives) , remove current one. have rocket 3 lives. reuse current rocket until dies.
Comments
Post a Comment