我正在尝试编写一个游戏,其中一个矩形根据屏幕底部的键盘输入移动,并且当按下空格键时,它将释放由另一个被弹起的矩形实现的炸弹。我有一个实现空格键启动炸弹的炸弹类。但是,每当我运行时,都会出现nullPointerException错误。这两个类的代码如下。帮助将不胜感激。
SuperLeaperPro4000类:
package SuperLeaperPro4000;
//need to import this;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class SuperLeaperPro4000 extends Canvas implements ActionListener, KeyListener{
//Declare and initialize timer
Timer animator = new Timer(1, this);
//Global variable declarations
//coordinates of first rectangle
int x= 0;
int y= 900;
//velocity variables of first rectangle
int velX= 0;
int velY= 0;
Graphics g;
public SuperLeaperPro4000() {
//add key listener
addKeyListener(this);
//enable key listener
setFocusable(true);
}
public static void main(String[] args){
//create a JFrame named Animation;
JFrame frame= new JFrame("Animation");
//create a new instance of the scribble class
Canvas canvas= new SuperLeaperPro4000();
//set the size of the canvas
canvas.setSize(1000, 1000);
frame.setResizable(false);
//add the canvas
frame.add(canvas);
//make sure window is maxed
frame.pack();
//stop program when closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//let the frame be seen
frame.setVisible(true);
}
public void paint (Graphics g) {
//draw the rectangle that will move according to keyboard inputs
g.drawRect(x, y, 50, 50);
animator.start();
}
public void actionPerformed(ActionEvent e) {
//update x
if (x+velX>=0 && x+velX<=950) {
x= x + velX;
}
//update y
if (y+velY>=0 && y+velY<=900) {
y= y + velY;
}
repaint();
}
//method that gets called whenever user presses keyboard key
public void keyPressed(KeyEvent key) {
//if user presses left arrow key, update left velocity;
if(key.getKeyCode()==KeyEvent.VK_LEFT) {
velX= -1;
}
//if user presses right arrow key, update right velocity;
else if (key.getKeyCode()==KeyEvent.VK_RIGHT) {
velX= 1;
}
//if user presses space, go up;
if (key.getKeyCode()==KeyEvent.VK_SPACE){
Bomb haymaker= new Bomb(x, y, 10, 10, 0, -5);
haymaker.paint(g);
}
}
//method that gets called whenever user releases keyboard key
public void keyReleased(KeyEvent key) {
//when the user releases the keys, set the velocities back to 0
if(key.getKeyCode()==KeyEvent.VK_LEFT || key.getKeyCode()==KeyEvent.VK_RIGHT) {
velX= 0;
}
}
public void keyTyped(KeyEvent key) {
}
}
炸弹类别:
package SuperLeaperPro4000;
//need to import this;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Bomb extends Canvas {
//field values
int x;
int y;
int height;
int width;
int velX;
int velY;
//constructor for a bomb
public Bomb(int x, int y, int height, int width, int velX, int velY) {
this.x= x;
this.y= y;
this.height= height;
this.width= width;
this.velX= velX;
this.velY= velY;
}
public void paint(Graphics gs) {
gs.drawRect(this.x, this.y, this.height, this.width);
}
public void fire(int velocity) {
this.velX= velocity;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
public int getVelX() {
return this.velX;
}
public int getVelY() {
return this.velY;
}
}