Hello everyone. I'm trying to program a counter that, everytime a bird gets to the edge of the world (runs away) , decreases one value to it.
But when the bird gets to the edge and its removed it gives me NullPointerException. Any help?
BirdWorld
BirdCounter
The Bird
public BirdWorld()
{
super(1000, 600, 1);
BirdCounter birdcounter = new BirdCounter();
addObject(birdcounter, 120, 57);
}
//
//
//
public void subScore()
{
birdcounter.subtractCount();
}public class BirdCounter extends Actor
{
private int birdCount = 10;
public BirdCounter()
{
setImage(new GreenfootImage(200,30));
update();
}
public void subtractCount()
{
birdCount--;
update();
}
public void update()
{
GreenfootImage img = getImage();
img.clear();
img.setColor(Color.BLACK);
img.drawString("Pássaros:" + birdCount,4,20);
}
} public void act()
{
BirdWorld mybirdworld = (BirdWorld) getWorld();
move(2);
if(atWorldEdge())
{
//getWorld().removeObject(this); i comment this to try to fix with turn
turn(180);
mybirdworld.subScore();
}
}
