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

2014/10/15

I keep getting a terminal error

coder04 coder04

2014/10/15

#
i get a terminal window with at Miniship.act(Miniship.java:9) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Miniship.fire(Miniship.java:21) at Miniship.act(Miniship.java:9) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Miniship.fire(Miniship.java:21) at Miniship.act(Miniship.java:9) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) My code import greenfoot.*; import java.util.List; /** */ public class Ship extends Actor { int speed = 5; int shotCounter = 0; private int delay = 0; private final int MAX_DELAY = 13; //private Bar bar; /** * Method act: move spaceship */ public void act() { move(); if(delay <= 0) { if( Greenfoot.isKeyDown("space") ) { fire(); Greenfoot.playSound("laser.wav"); delay = MAX_DELAY; } } else { delay--; } } /** * Fire the laser */ private void fire() { Shot shot = new Shot(); getWorld().addObject(shot, getX(), getY()); getWorld().addObject(shot, getX(), getY()); shot.setRotation(getRotation()); } /** * Method move: checks for keystrokes and applies the changes, then moves the ship. * I applied a bit of slowing to the ship's speed (so it would be drifting to a stop) */ private void move() { int dz = 0; if (Greenfoot.isKeyDown("d")) dz++; if (Greenfoot.isKeyDown("a")) dz--; setRotation(getRotation() + dz * 5); int ds = -1; if (Greenfoot.isKeyDown("w")) ds += 2; speed += ds; if (speed < 0) speed = 0; if (speed > 90) speed = 90; if (speed >= 200) move(speed / 100); if (Greenfoot.isKeyDown("s")) ds += 5; speed += ds; if (speed < 5) speed = 5; if (speed > 200) speed = 200; if (speed >= 20) move(speed / 10); } }
NikZ NikZ

2014/10/15

#
I don't think you can add the same thing twice.
coder04 coder04

2014/10/15

#
what did i do twice
NikZ NikZ

2014/10/15

#
Adding shot twice. Please use the code tag.
danpost danpost

2014/10/15

#
Adding the actor twice will not throw an exception. The error is occurring in the MiniShip class, not the Ship class. Please show the code for that class; particularly the 'fire' and 'act' methods of that class. Use the 'code' link below the 'Post a reply' input box to insert your code into any posts.
NikZ NikZ

2014/10/15

#
danpost wrote...
Adding the actor twice will not throw an exception. The error is occurring in the MiniShip class, not the Ship class. Please show the code for that class; particularly the 'fire' and 'act' methods of that class. Use the 'code' link below the 'Post a reply' input box to insert your code into any posts.
Yeah, but if you want to have two shots, don't do that.
You need to login to post a reply.