This site requires JavaScript, please enable it in your browser!
Greenfoot back
tomcoleman1994
tomcoleman1994 wrote ...

2012/4/27

my rocket wont fire :(

tomcoleman1994 tomcoleman1994

2012/4/27

#
i have made two classes Bullet and Rocket, Bullet fires but Rocket does not. the code is the same apart from the classes. not too sure why its not working. Any ideas?... /** * Fire bullet */ public void fireBullet() { Bullet bullet = new Bullet(); getWorld().addObject(bullet, getX(),getY() ); bullet.setRotation(getRotation()); bullet.move(45.0); } /** * Fire Rocket */ public void fireRocket() { Rocket rocket = new Rocket(); getWorld().addObject(rocket, getX(),getY() ); rocket.setRotation(getRotation()); rocket.move(40.0); }
davmac davmac

2012/4/27

#
What exactly do you mean when you say that the rocket doesn't fire - does it appear but not move, or appear and immediately disappear, or not seem to appear at all? Also, what does the 'act()' method of Rocket do? (and how does this compare with the same method in Bullet?)
tomcoleman1994 tomcoleman1994

2012/4/27

#
It does not appear at all. the Bulet class is as follows... public class Rocket extends Mover { private int life = Greenfoot.getRandomNumber(10) + 10; /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(25.0); life--; if(life == 0) { getWorld().addObject(new Crater(), getX(), getY()); getWorld().addObject(new MushroomCloud(), getX(), getY()); getWorld().removeObject(this); } }
davmac davmac

2012/4/27

#
Do you actually call the fireRocket() method from somewhere? Could you post that code?
tomcoleman1994 tomcoleman1994

2012/4/27

#
would you like me so sent the zip to you?
davmac davmac

2012/4/27

#
No, just post the code here, or upload your scenario and post a link to it.
davmac davmac

2012/4/27

#
* And, when you post code here, use code tags. Click the 'code' link/button below the text edit area and paste your code in to the box that pops up. Finally, please fix the indentation first.
tomcoleman1994 tomcoleman1994

2012/4/27

#
http://www.greenfoot.org/scenarios/4935
davmac davmac

2012/4/27

#
Ignoring comments, you have code that looks like this:
       if(",".equals(Greenfoot.getKey()))
       {
           fireBullet();
       }
        
       if("enter".equals(Greenfoot.getKey()))
       {
           fireRocket();
       }
You're calling 'Greenfoot.getKey()' twice. Each time you call getKey() it discards a key, so, this can't work. You need to either call it once and save the result in a variable (then compare the variable to the key names) or use isKeyDown(...) instead.
tomcoleman1994 tomcoleman1994

2012/5/1

#
ok thanks i have used the isKeyDown Method now and all is ok, thanks
You need to login to post a reply.