我的任务是创建游戏。我想在白细胞与病毒对象碰撞时添加分数,但是当两个物体碰撞并且要添加分数时,它会给我一个空的指向性异常,并且不会增加分数。 >
public class VirusGame extends Application{
Pane root;
Canvas canvas;
GraphicsContext gc;
Scene scene;
ArrayList<GameObject> cell = new ArrayList<GameObject>();
static ArrayList<GameObject> vir = new ArrayList<GameObject>();
static ArrayList<GameObject> wbcm = new ArrayList<GameObject>();
Random rnd = new Random(System.currentTimeMillis());
Random rand = new Random();
int virusCount = 0;
WBC wbc;
WBCM miss;
int width, height;
int score = 0;
int health = 100;
Stage window;
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
gc.fillRect(0,0,canvas.getWidth(),canvas.getHeight());
if(virusCount++ > 70) {
vir.add(new Virus(gc, rand.nextInt(950), -50 + -rand.nextInt(100)));
virusCount = 10;
}
for(GameObject obj : vir)
{
obj.update();
}
for(GameObject obj : wbcm)
{
obj.update();
}
for(GameObject obj : cell)
{
obj.update();
}
} };
Factory factory;
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
public void start(Stage stage) throws Exception {
window = stage;
Text text = new Text();
text.setText("Score: " + score);
text.setX(50);
text.setY(50);
stage.setTitle("VirusAttack");
Pane root = new Pane();
width = (int) 1100;
height = (int) 900;
canvas = new Canvas(1100,900);
gc = canvas.getGraphicsContext2D();
gc.setFill(Color.LIGHTCYAN);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
root.getChildren().addAll(canvas, text);
factory = new Factory(gc);
cell.add((wbc = (WBC)factory.createProduct("WBC", 500, 850)));
scene = new Scene(root, width, height);
scene.addEventHandler(KeyEvent.KEY_PRESSED,(key)->{
if(key.getCode() == KeyCode.LEFT) {
wbc.setSpeedX(4);
wbc.setDirectionX(-1);
}else if (key.getCode() == KeyCode.RIGHT) {
wbc.setSpeedX(4);
wbc.setDirectionX(1);
}else if (key.getCode() == KeyCode.SPACE) {
double x = wbc.x;
double y = wbc.y;
wbcm.add(new WBCM(gc, wbc.returnX(x) , wbc.returnY(y) ));
}
});
scene.addEventHandler(KeyEvent.KEY_RELEASED,(key)->{
if(key.getCode() == KeyCode.LEFT) {
wbc.setSpeedX(0);
wbc.setDirectionX(0);
}else if (key.getCode() == KeyCode.RIGHT) {
wbc.setSpeedX(0);
wbc.setDirectionX(0);
}
});
window.setScene(scene);
window.setResizable(false);
window.show();
timer.start();
}
public static ArrayList<GameObject> getVirusList(){
return vir;
}
public static void removeVirus(GameObject tempVir) {
vir.remove(tempVir);
}
public static ArrayList<GameObject> getWBCMList(){
return wbcm;
}
public static void removeWBCM(GameObject tempWbcm) {
wbcm.remove(tempWbcm);
}
public void endGame() {
if(health == 0) {
window.close();
}
}
}
class Virus extends GameObject{
public Virus(GraphicsContext gc, double x, double y) {
super(gc, x, y);
img=new Image("res/virus.png");
}
public void update() {
super.y += 3;
super.update();
}
}
class WBCM extends GameObject{
VirusGame vg;
public WBCM(GraphicsContext gc, double x, double y) {
super(gc, x, y);
gc.drawImage(img, x, y, 20, 20);
img=new Image("res/WBCM.png");
}
public void checkCollisions() {
ArrayList<GameObject> vir = VirusGame.getVirusList();
for(int i = 0; i < vir.size(); i++) {
GameObject tempVir = vir.get(i);
if(getBoundingBox().intersects(tempVir.getBoundingBox())) {
VirusGame.removeVirus(tempVir);
vg.score = vg.score + 20;
}
}
}
public void update() {
super.y += -4;
checkCollisions();
super.update();
}
public int returnScore (int score) {
return score;
}
}
class GameObject {
protected Image img;
protected double x, y;
protected GraphicsContext gc;
public GameObject(GraphicsContext gc, double x, double y)
{
this.gc=gc;
this.x=x;
this.y=y;
}
public void update()
{
if(img!=null)
gc.drawImage(img, x, y, 40, 40);
}
public BoundingBox getBoundingBox() {
return new BoundingBox(x, y, 50,50);
}
}
我希望每次接触物体时得分都会增加20,但是当它们接触时,我会得到一个空指针异常