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

2013/4/24

Asteroid

1
2
infant17 infant17

2013/4/24

#
I need help with an error message I'm getting. I'm new to greenfoot and understand it a little bit right now Im trying to write a code where the rocket is able to shoot photon torpedos by pressing the letter p...and the photon torpedo completely destroy just one asteroid. Im trying to work on the first command and Im getting an error message that says "cannot find symbol - method fire Boolean....I know I have to declare fire but when I do I still get the same error message. This is what I have so far: /** * A rocket that can be controlled by the arrowkeys: up, left, right. * The gun is fired by hitting the 'space' key. 'z' releases a proton wave. * * @author Poul Henriksen * @author Michael Kolling * * @version 1.0 */ public class Rocket extends SmoothMover { public int torpedoCount = 10; public int torpedoDamage = 10; private static final int gunReloadTime = 5; // The minimum delay between firing the gun. private static final int protonReloadTime = 500; // The minimum delay between proton wave bursts. private int reloadDelayCount; // How long ago we fired the gun the last time. private int protonDelayCount; // How long ago we fired the proton wave the last time. private GreenfootImage rocket = new GreenfootImage("rocket.png"); private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png"); /** * Initilise this rocket. */ public Rocket() { reloadDelayCount = 10; protonDelayCount = 10; addForce(new Vector(13, 0.3)); // initially slowly drifting } /** * Do what a rocket's gotta do. (Which is: mostly flying about, and turning, * accelerating and shooting when the right keys are pressed.) */ public void act() { move(); checkKeys(); checkCollision(); reloadDelayCount++; protonDelayCount++; } /** * Check whether there are any key pressed and react to them. */ private void checkKeys() { ignite(Greenfoot.isKeyDown("up")); if (Greenfoot.isKeyDown("left")) { setRotation(getRotation() - 5); } if (Greenfoot.isKeyDown("right")) { setRotation(getRotation() + 5); } if (Greenfoot.isKeyDown("z")) { fire(); } if (Greenfoot.isKeyDown("u"))// { startProtonWave(); } if( Greenfoot.isKeyDown("space") || Greenfoot.isKeyDown("c") ) { ((Space)getWorld()).fire(false); } if( Greenfoot.isKeyDown("p") ) { ((Space)getWorld()).fire(true); } if (Greenfoot.isKeyDown("s"))//JC.....When letter s is pressed rocket stops { Greenfoot.stop(); //JC......line satisfies R4 requirement. } } /** * third and final firing method, linked from the world which is linked from buttonFirePhaser and buttonFireTorpedo */ public void fire ( boolean isTorpedo ) { if ( isTorpedo ) { if( torpedoCount >0 ) { if (reloadDelayCount >= reloadTime) { Torpedo torpedo = new Torpedo(torpedoDamage); getWorld().addObject(torpedo, getX(), getY()); torpedo.setRotation(getRotation()); reloadDelayCount = 0; torpedoCount--; counterTorpedo.setValue(torpedoCount); Greenfoot.playSound("Torpedo.mp3"); } } } else { if (phaserDelayCount >= phaserReloadTime) { Phaser phaser = new Phaser(phaserDamage); getWorld().addObject(phaser, getX(), getY()); phaser.setRotation(getRotation()); phaserDelayCount = 0; Greenfoot.playSound("Phaser.wav"); } } } /** * Check whether we are colliding with an asteroid. */ private void checkCollision() { Actor a = getOneIntersectingObject(Asteroid.class); if (a != null) { Space space = (Space) getWorld(); space.addObject(new Explosion(), getX(), getY()); space.removeObject(this); space.gameOver(); } } /** * Should the rocket be ignited? */ private void ignite(boolean boosterOn) { if (boosterOn) { setImage (rocketWithThrust); addForce (new Vector(getRotation(), 0.3)); } else { setImage(rocket); } } /** * Fire a bullet if the gun is ready. */ private void fire() { if (reloadDelayCount >= gunReloadTime) { Bullet bullet = new Bullet (getMovement().copy(), getRotation()); getWorld().addObject (bullet, getX(), getY()); bullet.move (); reloadDelayCount = 0; } } /** * Release a proton wave (if it is loaded). */ private void startProtonWave() { if (protonDelayCount >= protonReloadTime) { ProtonWave wave = new ProtonWave(); getWorld().addObject (wave, getX(), getY()); protonDelayCount = 10;//JC } } }
willnerd42 willnerd42

2013/4/24

#
I tried to find your problem, but there was to much code. Please indicate where the error is.
infant17 infant17

2013/4/24

#
error ..... if( Greenfoot.isKeyDown("space") || Greenfoot.isKeyDown("c") ) { ((Space)getWorld()).fire(false); } if( Greenfoot.isKeyDown("p") ) { ((Space)getWorld()).fire(true);
infant17 infant17

2013/4/24

#
I really don't understand what im doing wrong
davmac davmac

2013/4/24

#
It looks like you are trying to call the fire(...) method in the Space class, but there is no such method in the Space class. From the looks of it, you probably just meant to call the fire(...) method in the current class (the Rocket class). In that case, instead of: ((Space)getWorld()).fire(false); Just write: fire(false); And similarly for the 'true' case.
infant17 infant17

2013/4/24

#
But if I just write fire(false) wouldn't that stop the rocket from firing the torpedos when pressing p
davmac davmac

2013/4/24

#
Maybe you've misunderstood me. In your present code, replace '((Space)getWorld()).fire(false);' with 'fire(false);' and replace '((Space)getWorld()).fire(true);' with 'fire(true)'. The latter will fire torpedoes. You still need the 'if' statement which controls which gets called.
infant17 infant17

2013/4/24

#
ok thank you......I understand now
infant17 infant17

2013/4/25

#
I am so lost on this error I'm getting. Someone please help..........Error im getting is method fire in class Rocket cannot be applied to given types: required: no arguments; found: Boolean; reason: actual and formal argument lists differ in length.
Well, it looks like you are trying to put a boolean into a method that takes no parameters. Where does it lead you when you click on the error?
Please type the line of code.
infant17 infant17

2013/4/25

#
Its taking me to the fire(false) line. } if( Greenfoot.isKeyDown("space") || Greenfoot.isKeyDown("c") ) { fire(false); } if( Greenfoot.isKeyDown("e") ) { fire(true); } }
Well, you're fire() method does not have a boolean parameter. You must have accidentally deleted it, or never had it in the first place. That is what the error is saying.
infant17 infant17

2013/4/25

#
not sure what that means......im a beginner at this
JetLennit JetLennit

2013/4/25

#
His fire does have a boolean
There are more replies on the next page.
1
2