We are trying to implement the Counter actor in Tutorial #6. When we get to the point in our game where we want to bump the score, we trap to a NullPointerException. We understand this means our instance pointer isn't working but we're not sure why.
In our World subclass, called "MyWorld" we have:
In our Counter class, we have:
and in our shooting object's act loop, when it touches the "enemy" we have:
public class MyWorld extends World
{
private Counter theCounter;
public MyWorld()
{
super(1150,600,1);
prepare();
}
public Counter getCounter()
{
return theCounter;
}
private void prepare()
{
Counter theCounter = new Counter();
addObject(theCounter, 61,63);
}
}
public class Counter extends Actor
{
int score = 0;
public void addScore()
{
score =score +1;
}
public void act()
{
setImage(new GreenfootImage("Score :",score);
}
}MyWorld = theWorld =(MyWorld) getWorld(); Counter theCounter = theWorld.getCounter(); theCounter.addScore();
