Hi,
I want to get access of a variable in another class, beause I want to have same rotation of my projectile as my shuttle.
Here is the code of the shuttle:
And here of my projectile:
I get this error:
Hope you can help me :)
import greenfoot.*; public class Shuttle extends Actor { private int speed; private int direction; private int cooldown; public int rotation; public Shuttle() { setRotation(90); speed=5; direction=5; cooldown=0; } public void act() { move1(); turn1(); shoot(); isTouching(); slowMotion(); rotationShuttle(); getRotationShuttle(); } public void move1() { if(Greenfoot.isKeyDown("W")) { move(speed * -1); } if(Greenfoot.isKeyDown("S")) { move(speed); } } public void turn1() { if(Greenfoot.isKeyDown("A")) { setRotation(getRotation() - direction); } if(Greenfoot.isKeyDown("D")) { setRotation(getRotation() + direction); } } public void shoot() { if(cooldown==0) { if(Greenfoot.isKeyDown("SPACE")) { Projectile projectile =new Projectile(); getWorld().addObject(projectile,getX(), getY()); cooldown=25; } } else { if(cooldown>0) { cooldown=cooldown-1; } } } public void isTouching() { if(isTouching(Ufo.class)) { Greenfoot.stop(); } } public void slowMotion() { if(Greenfoot.isKeyDown("Q")) { Greenfoot.delay(3); ((Cash)getWorld().getObjects(Cash.class).get(0)).bumpCount(-40); } } public void rotationShuttle() { rotation=getRotation(); } public int getRotationShuttle() { return rotation; } }
import greenfoot.*; public class Projectile extends Shuttle { private int speedProjectile; private int rotation; public Projectile() { speedProjectile=8; int rotation = ((Shuttle) getWorld().getObjects(Shuttle.class).get(0)).getRotationShuttle(); setRotation(rotation); } public void act() { projectileMove(); border(); } public void projectileMove() { move(speedProjectile); } public void border() { if(getX()>860 || getX()<0 || getY()>860 || getY()<0) { getWorld().removeObject(this); } } public void test() { int rotationShuttle = ((Shuttle) getWorld().getObjects(Shuttle.class).get(0)).getRotationShuttle(); setRotation(rotationShuttle); } }
java.lang.NullPointerException at Projectile.<init>(Projectile.java:11) at Space.<init>(Space.java:18) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at greenfoot.core.Simulation.newInstance(Simulation.java:596) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:437) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:483) at greenfoot.core.Simulation.maybePause(Simulation.java:296) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Projectile.<init>(Projectile.java:11) at Space.<init>(Space.java:18) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at greenfoot.core.Simulation.newInstance(Simulation.java:596) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:437) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:483) at greenfoot.core.Simulation.maybePause(Simulation.java:296) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205)