我正在尝试碰撞我以为我得到了它但显然不是这样当我运行游戏时我得到了一个错误..这真的很烦人。所以我来这里是为了得到一些帮助:D希望:P 无论如何,我一直在收到这个错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.shiny21.graphics.GFX.step(GFX.java:37)
at com.shiny21.graphics.GFX.actionPerformed(GFX.java:65)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我还没有真正理解错误,但是代码为GFX:
package com.shiny21.graphics;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
import com.shiny21.framework.Collision;
import com.shiny21.players.PlayerB;
import com.shiny21.players.PlayerR;
public class GFX extends JPanel implements ActionListener{
PlayerR red;
PlayerB blue;
Collision col;
public GFX(){
red = new PlayerR(getX(), getY());
blue = new PlayerB(getX(), getY());
this.setFocusable(true);
this.requestFocus();
setBackground(Color.BLACK);
Timer timer = new Timer(1000 / 60, this);
timer.start();
}
public void step(){
col.CollisionStep();
red.step();
blue.step();
//System.out.println("");
}
public void paintComponent(Graphics g){
super.paintComponent(g);
//RED
g.setColor(Color.RED);
g.fillRect(red.getX(), red.getY(), 8, 8);
g.setColor(Color.WHITE);
g.drawRect(red.getX()+20 - red.getSightW(), red.getY()+20 - red.getSightH(), red.getSightW(), red.getSightH());
//BLUE
g.setColor(Color.BLUE);
g.fillRect(blue.getX(), blue.getY(), 8, 8);
g.setColor(Color.GREEN);
g.drawRect(blue.getX()+20 - blue.getSightW(), blue.getY()+20 - blue.getSightH(), blue.getSightW(), blue.getSightH());
}
public void actionPerformed(ActionEvent ae) {
step();
repaint();
}
}
那就是碰撞代码:
package com.shiny21.framework;
import java.awt.Rectangle;
import com.shiny21.players.PlayerB;
import com.shiny21.players.PlayerR;
public class Collision {
PlayerB blue;
PlayerR red;
public Collision(){
CollisionStep();
}
public void CollisionStep(){
Rectangle r1 = blue.getBounds();
Rectangle r2 = red.getBounds();
if(r1.intersects(r2)){
blue.setDX(-1);
red.setDX(1);
}
}
}
编辑1:
我现在收到此错误
Exception in thread "main" java.lang.NullPointerException
at com.shiny21.framework.Collision.<init>(Collision.java:14)
at com.shiny21.graphics.GFX.<init>(GFX.java:25)
at com.shiny21.framework.Game.main(Game.java:18)
然后我将其添加到Collision类:
blue = new PlayerB(blue.getX(), blue.getY());
red = new PlayerR(red.getX(), red.getY());
答案 0 :(得分:0)
您的字段col
为空且从未设置。这一行col.CollisionStep();