【发布时间】:2018-02-19 03:40:05
【问题描述】:
大家! 我又来惹你生气了)
所以,我试图制作一个游戏,我描述了一些类: GameObject - 只是一个具有 X,Y,Sizes,Name 并且可以绘制和移动的对象:
package pkg3dgraphics;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class GameObject {
protected String name;
protected int xCoord,yCoord,mySizeX = 25,mySizeY = 25;
public GameObject(){
}
public GameObject(String newName, int startXCoord, int startYCoord){
setName(newName);
setX(startXCoord);
setY(startYCoord);
}
public void SetSize(int X, int Y){
mySizeX = X;
mySizeY = Y;
}
public int getXsize(){
return mySizeX;
}
public int getYsize(){
return mySizeY;
}
public String getName(){
return name;
}
public void setName(String newName){
name = newName;
}
public int getX(){
return xCoord;
}
public int getY(){
return yCoord;
}
public void setX(int newXCoord){
xCoord = newXCoord;
}
public void setY(int newYCoord){
yCoord = newYCoord;
}
public void moveTo(int X,int Y){
xCoord = X;
yCoord = Y;
}
public void moveBy(int X,int Y){
xCoord +=X;
yCoord +=Y;
}
public void Draw(GraphicsContext currentContext){
currentContext.setFill(Color.GREEN);
currentContext.setStroke(Color.BLUE);
currentContext.fillOval(xCoord,yCoord, mySizeX,mySizeY );
}
}
我让 Shooter 扩展了之前的课程,然后他就来了:
package pkg3dgraphics;
public class Shooter extends GameObject {
public Shooter(){
}
public Shooter(String newName, int startXCoord, int startYCoord){
setName(newName);
setX(startXCoord);
setY(startYCoord);
}
public Bullet shoot(String direction){
Bullet newBullet = new Bullet(direction);
return newBullet;
}
}
我还有子弹,你知道,子弹:
package pkg3dgraphics;
import javafx.scene.paint.Color;
import javafx.scene.canvas.GraphicsContext;
public class Bullet extends GameObject{
String myDirection;
protected int mySpeed = 5,mySizeX = 5,mySizeY = 5;
boolean goNorth,goSouth,goWest,goEast;
protected boolean active = false;
public void setSpeed(int newSpeed){
mySpeed = newSpeed;
}
public void setDirection(String newDirection){
myDirection = newDirection;
active = true;
if ( myDirection == "North" )
goNorth = true;
if ( myDirection == "South" )
goSouth = true;
if ( myDirection == "West" )
goWest = true;
if ( myDirection == "East" )
goEast = true;
}
Bullet(String direction ){
myDirection = direction;
active = true;
if ( myDirection == "North" )
goNorth = true;
if ( myDirection == "South" )
goSouth = true;
if ( myDirection == "West" )
goWest = true;
if ( myDirection == "East" )
goEast = true;
}
public void advance(int W,int H,GraphicsContext gc){
if (xCoord <= W && xCoord >= 0 && yCoord <= H && yCoord >= 0 ) {
if (goNorth) moveBy(0,mySpeed);
if (goSouth) moveBy(0,mySpeed);
if (goWest) moveBy(mySpeed,0);
if (goEast) moveBy(mySpeed,0);
}else{
active = false;
Vanish(gc);
goNorth = false;
goSouth = false;
goWest = false;
goEast = false;
}
}
public void Vanish(GraphicsContext gc){
gc.setFill(Color.WHITE);
}
}
目的是让这家伙以另一种方式射击: 当我抓住按下的按钮时,我使用预先创建的非活动项目符号并将其引导到多个方向。 当此项目符号越过窗口的边界时,它会变为非活动状态并停止。 为此,我有一系列子弹。 当用户按下射击键时,我在数组中寻找一个非活动的子弹,如果没有,我创建一个遵循用户想要的路径的额外子弹。 好吧,这种实现子弹的方式可能不是最好的,但我没有想出另一个。
所以我编译了我的主类:
package pkg3dgraphics;
import java.util.Vector;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import static javafx.scene.input.KeyCode.DOWN;
import static javafx.scene.input.KeyCode.LEFT;
import static javafx.scene.input.KeyCode.RIGHT;
import static javafx.scene.input.KeyCode.SHIFT;
import static javafx.scene.input.KeyCode.UP;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
public int WINDOWWIDTH = 600;
public int WINDOWHEIGHT = 400;
boolean goNorth,goSouth,goWest,goEast,running,ShootNorth,ShootSouth,ShootWest,ShootEast;
Shooter player = new Shooter("Player",WINDOWWIDTH/2,WINDOWHEIGHT/2);
Bullet bullets[] = new Bullet[500];
int bulletsSize = 0;
Canvas mainCanvas = new Canvas(WINDOWWIDTH,WINDOWHEIGHT);
GraphicsContext mainContext = mainCanvas.getGraphicsContext2D();
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Canvas mainCanvas = new Canvas(WINDOWWIDTH,WINDOWHEIGHT);
GraphicsContext mainContext = mainCanvas.getGraphicsContext2D();
root.getChildren().add(mainCanvas);
Scene scene = new Scene(root,WINDOWWIDTH,WINDOWHEIGHT);
scene.setOnKeyPressed(new EventHandler<KeyEvent> (){
@Override
public void handle(KeyEvent event){
switch(event.getCode()){
case UP: ShootNorth = true; break;
case DOWN: ShootSouth = true; break;
case LEFT: ShootWest = true; break;
case RIGHT: ShootEast = true; break;
case W: goNorth = true; break;
case S: goSouth = true; break;
case A: goWest = true; break;
case D: goEast = true; break;
}
}
});
scene.setOnKeyReleased(new EventHandler <KeyEvent>(){
@Override
public void handle(KeyEvent event){
switch(event.getCode()){
case UP: ShootNorth = false; break;
case DOWN: ShootSouth = false; break;
case LEFT: ShootWest = false; break;
case RIGHT: ShootEast = false; break;
case W: goNorth = false; break;
case S: goSouth = false; break;
case A: goWest = false; break;
case D: goEast = false; break;
}
}
});
primaryStage.setScene(scene);
primaryStage.show();
AnimationTimer Timer = new AnimationTimer(){
@Override
public void handle (long now){
int dx = 0, dy = 0;
if (goNorth) dy = -1;
if (goSouth) dy = 1;
if (goWest) dx = -1;
if (goEast) dx = 1;
if (running) { dx *= 3; dy *= 3;}
mainContext.clearRect(0,0,WINDOWWIDTH,WINDOWHEIGHT);
player.moveBy(dx, dy);
CheckShoot();
player.Draw(mainContext);
}
};
Timer.start();
}
public void CheckShoot(){
String direction = null;
int count = 0;
if (ShootNorth)
{
direction = "North";
}
if (ShootSouth)
{
direction = "South";
}
if (ShootWest)
{
direction = "West";
}
if (ShootEast)
{
direction = "East";
}
for (int i = 0; i < bulletsSize; i ++ )
{
if (bullets[i].active = false ){
bullets[i].setDirection(direction);
bullets[i].moveTo(player.getX(),player.getY());
break;
}else count ++;
}
if ( count == bulletsSize ) {
bulletsSize++;
bullets[bulletsSize] = player.shoot(direction);
}
}
public void advanceAll(){
for (int i = 0; i < bulletsSize; i ++ )
{
bullets[i].advance(WINDOWWIDTH,WINDOWHEIGHT,mainContext);
}
}
}
应用程序运行,但我得到这个问题循环,直到我关闭应用程序:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at pkg3dgraphics.Main.CheckShoot(Main.java:126)
at pkg3dgraphics.Main$3.handle(Main.java:91)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.lambda$handle$483(AnimationTimer.java:57)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.handle(AnimationTimer.java:56)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:357)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
所以,这就是我想要抱怨的全部内容。 如果有人知道为什么会发生这些事情,如果您与我分享这个秘密,我会很高兴和感激。(以及所有其他人)
【问题讨论】:
-
不需要秘密或魔法。该错误是不言自明的。它属于 NullPointerException 类型,位于 CheckShoot 方法的第 126 行。您需要进行空值检查或确保这些字段永远不会为空。