Hey guys i have been up all night, I have a side scroll game where you can walk back and forth among the world. the actor walks with WASD when I press a for the character to walk back I get a runtime error (nullpointer)
The problem is that I have a different class for the character when it faces different ways (moving forward and back) I also have a different classes for each weapon (the way it faces) as I move -------> with "d" and the other way with "a". Below is one of my Weapon classes. As one can see I have "a" removing this actor because "a" makes the character turnaround. In another class "a" adds another actor( a turned around version of the Gun actor in the code below). So I was thinking of ways to simplify this and I want to make sure this idea is possible. Can I just make one class for each actor. For example one class for both guns and when "a" is pressed it sets an image to a turned around version of the Gun (which is possible) but would the gun then shoot in that direction it would face?
Thanks for reading this sorry if it does not make sense I am pretty tired, haha
public class Gun extends Weapons { private Spacemarinemodel1 spacemarinemodel1; public Gun(Spacemarinemodel1 spacemarinemodel1) { this.spacemarinemodel1 = spacemarinemodel1; } public void act() { if("l".equals(Greenfoot.getKey())) { if(getWorld().getObjects(Bullet1.class).size()<=3) fire(); } if((Greenfoot.isKeyDown("a"))) { getWorld().removeObject(this); } //switch weapon to Sniper if((Greenfoot.isKeyDown("2"))) getWorld().addObject(new Sniper(spacemarinemodel1), getX(), getY()); //remove the first weapon if((Greenfoot.isKeyDown("2"))) {getWorld().removeObject(this); } int spacemarinemodel1X = spacemarinemodel1.getX(); int spacemarinemodel1Y = spacemarinemodel1.getY(); // Modify the xOffset and // yOffset to make the gun // appear in the correct // position. int xOffset = 27; int yOffset = -8; int x = spacemarinemodel1X + xOffset; int y = spacemarinemodel1Y + yOffset; setLocation(x, y); } /** * fire the gun */ private void fire() { Ammo Bullet1 = new Bullet1(); getWorld().addObject(Bullet1, getX() , getY()); Bullet1.move(45); } }