Hello people,
At first my problem was that my score would always remain zero even when I touched objects that were supposed to change the score. So, I changed a few things and now I always get this error :
java.lang.NullPointerException
at Spieler.act(Spieler.java:62) ------> This is when it is supposed to addScore
at greenfoot.core.Simulation.actActor(Simulation.java:604)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
at greenfoot.core.Simulation.runContent(Simulation.java:221)
at greenfoot.core.Simulation.run(Simulation.java:211)
java.lang.NullPointerException
at Spieler.act(Spieler.java:55) ------->here it should be -1
at greenfoot.core.Simulation.actActor(Simulation.java:604)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
at greenfoot.core.Simulation.runContent(Simulation.java:221)
at greenfoot.core.Simulation.run(Simulation.java:211)
I am very new to this so please keep it simple and if my code doesn't really make sense that's why :D
I would really appreciate help!!
Also, I have a few comments/aspects in my code that might seem unnecessary to you...that's because I've already tried different ways to make it work, but nothing really fixed it
public MyWorld()
{
super(800, 600, 1);
setBackground("milky-way.jpg");
this.addObject(new Gegner(), 200,100);
this.addObject(new Sammel(),80,90);
Punktestand Punkt= new Punktestand();
this.addObject(Punkt,110,60);
Spieler S = new Spieler();
this.addObject(S, 300,300);
}
}
public class Punktestand extends Actor
{
/**
* Act - do whatever the Punktestand wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int Score=0;
GreenfootImage bild = new GreenfootImage(110,60);
public void update()
{
this.setImage (bild);
//this.S=Spiel;
this.getImage().clear();
this.getImage().setColor(Color.RED);
this.getImage().setFont(new Font("Verdana",20));
this.getImage().drawRect(0,0,bild.getWidth()-1,bild.getHeight()-1);
this.getImage().drawString("Punkte:" + Score, 12,35);
}
public void addScore()
{
Score++;
}
public void subtractScore()
{
Score--;
}
public Punktestand ()
{
update();
}
public void act()
{
this.getImage().clear();
update();
}
}
public class Spieler extends Actor
{
/**
* Act - do whatever the Spieler wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
// int Score = 0;
public Punktestand Punkt;
public Punktestand getPunktestand()
{
return Punkt;
}
public void act()
{
if(Greenfoot.isKeyDown("right")){
setRotation (0);
this.move (5); }
if(Greenfoot.isKeyDown("left")){
setRotation (180);
this.move (5);}
if (Greenfoot.isKeyDown("up")){
setRotation (270);
this.move (5); }
if (Greenfoot.isKeyDown ("down")){
setRotation (90);
this.move (5); }
if(isTouching (Gegner.class))
{
this.Punkt.subtractScore();
Greenfoot.playSound("gegner.mp3");
}
if(isTouching(Sammel.class))
{
this.Punkt.addScore();
Greenfoot.playSound("sammel.wav");
}
}
}
