Im trying increment a score to one when a bullet collides with a baddie. But i get this error when i try to:
java.lang.NullPointerException
at baddie.pop(baddie.java:34)
at bullet.collide_with_ballon(bullet.java:44)
at bullet.act(bullet.java:26)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
Here are my classes:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class character extends Actor
{
public void act()
{
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse !=null)
setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI));
move(speed);
if(Greenfoot.mouseClicked(null))
{
//shoot();
getWorld().addObject(new bullet(getRotation()),getX(),getY());
turnTowards(mouse.getX(), mouse.getY());
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
public class scoreBoard extends Actor
{
public static final float FONT_SIZE=48.0f;
public static final int WIDTH=400;
public static final int HEIGHT=300;
public scoreBoard()
{
this(100);
}
public scoreBoard(int score)
{
makeImage("Game Over", "Score: ",score);
}
private void makeImage(String title, String prefix,int score)
{
GreenfootImage image = new GreenfootImage(WIDTH,HEIGHT);
image.setColor(new Color(255,255,255, 128));
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(new Color(0, 0, 0, 128));
image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
Font font = image.getFont();
font = font.deriveFont(FONT_SIZE);
image.setFont(font);
image.setColor(Color.WHITE);
image.drawString(title, 60, 100);
image.drawString(prefix + score, 60, 200);
setImage(image);
}
public void act()
{
// Add your action code here.
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class baddieWorld extends World
{
private int score;
Counter counter = new Counter("Score: ");
/**
* Constructor for objects of class balloonWorld.
*
*/
public void countPop()
{
counter.add(1);
}
public baddieWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
populate();
}
private void populate()
{
addObject(counter,350,50);
}
public void act()
{
if(Greenfoot.getRandomNumber(100) < 3) {
addObject(new baddie(), Greenfoot.getRandomNumber(700), 600);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
public class bullet extends Actor
{
private int direction;
Counter c= new Counter();
baddie b = new baddie();
int score =0;
/**
* Act - do whatever the firedArrow wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
//setLocation(getX()+5,getY());
move(5);
colision();
}
public bullet(int dir)
{
setRotation(dir);
}
public void collision()
{
Actor baddie=getOneIntersectingObject(baddie.class);
if(baddie !=null)
{
getWorld().removeObject(baddie);
getWorld().removeObject(this);
b.pop();
}
}
public class baddie extends Actor
{
private int score;
public void act()
{
// Add your action code here.
setLocation(getX(), getY()-1);
}
public void pop()
{
((baddieWorld) getWorld()).countPop();((baddieWorld) getWorld()).countPop();
}
}
Any suggestions?