这是我第一次在这里发帖,如果我犯了任何错误就很抱歉。 :/
好的,我之前从未尝试过制作游戏之前曾经使用过java的BIT。 我试着用Wall制作一个简单的平台游戏,但它们对我来说很难。 我现在有一个简单的代码设置:
人们想要堆栈跟踪..(旧的我修复了这个,但我得到了新的错误,请参阅进一步了解)
java.lang.NullPointerException
at Start.start(Start.java:27)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
(旧)例外:
java.lang.NullPointerException
at level.getStartDirt(level.java:9)
at Start.start(Start.java:28)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
最新的堆栈跟踪:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at Start.paint(Start.java:75)
at Start.update(Start.java:67)
at sun.awt.RepaintArea.updateComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$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)
第75行:
level.paint(g);
第68行:
paint(doubleG);
开始上课:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Start extends Applet implements Runnable, KeyListener{
private Image i;
private Graphics doubleG;
//--Square s1;
Dirt dirt[];
level lvl;
@Override
public void init() {
this.setSize(800, 600);
addKeyListener(this);
}
@Override
public void start(){
level lvl = new level();
Thread thread = new Thread(this);
thread.start();
lvl.getStartDirt();
}
@Override
public void run() {
//thread information
while(true){
//--s1.update(this);
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void destroy() {
}
@Override
public void update(Graphics g) {
if(i == null)
{
i = createImage(this.getWidth(), this.getHeight());
doubleG = i.getGraphics();
}
doubleG.setColor(getBackground());
doubleG.fillRect(0, 0, this.getWidth(), this.getHeight());
doubleG.setColor(getForeground());
paint(doubleG);
g.drawImage(i, 0, 0, this);
}
@Override
public void paint(Graphics g) {
lvl.getPaintDirt(g);
//--s1.paint(g);
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
Sqaure课程:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Square {
private int x = 500;
private int y = 25;
private int width = 50;
private int height = 50;
private double dx = 0;
private double dy = 0;
private double gravity = 15;
private double energyloss = .65;
private double xFriction = .9;
private double dt = .2;
public Square(int sx, int sy){
x = sx;
y = sy;
}
public void update(Start sp){
if (x < 0){
x = 0;
dx = -dx;
}else if (x > sp.getWidth() - 50){
x = sp.getWidth() - 50;
dx = -dx;
}else{
x += dx;
}
if(y == sp.getHeight() - 50 -1){
dx *= xFriction;
if (Math.abs(dx) < .8){
dx = 0;
}
}
//gravity
//if(y > sp.getHeight() - 51){
// y = sp.getHeight() - 51;
// dy *= energyloss;
// dy *= -1;
//}else{
// if (Math.abs(dy)< (gravity/dt)/5.25 && y == sp.getHeight() - 51){
// dy = 0;
// y = sp.getHeight() - 51;
// }
//position formula
y += dy*dt + .5*gravity*dt*dt;
dy += gravity *dt;
}
public Rectangle getBoundries(){
Rectangle rect = new Rectangle(x, y, width, height);
return rect;
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(x, y, 50, 50);
}
}
污垢等级:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Dirt {
int x,y,width,height;
public Dirt(int sx, int sy){
x = sx;
y = sy;
width = 50;
height = 50;
}
public void destroy() {
}
public void paint(Graphics g) {
g.setColor(Color.orange);
g.fillRect(x, y, 50, 50);
}
public Rectangle getBoundries(){
Rectangle rect = new Rectangle(x, y, width, height);
return rect;
}
}
级别课程:
import java.awt.Graphics;
public class level {
Dirt d[] = new Dirt[9];
public Dirt[] getStartDirt(){
d[1] = new Dirt(400, 600);
d[1] = new Dirt(0, 600);
d[2] = new Dirt(50, 600);
d[3] = new Dirt(100, 600);
d[4] = new Dirt(150, 600);
d[5] = new Dirt(200, 600);
d[6] = new Dirt(250, 600);
d[7] = new Dirt(300, 600);
d[8] = new Dirt(350, 600);
return d;
}
public void getPaintDirt(Graphics g){
d[0].paint(g);
d[1].paint(g);
d[2].paint(g);
d[3].paint(g);
d[1].paint(g);
d[4].paint(g);
d[5].paint(g);
d[6].paint(g);
d[7].paint(g);
d[8].paint(g);
}
}
还有一些问题: - 运行时出现NullPointerException,为什么/它是什么意思? - 有更简单的方法在lvl中制作多个Dirt thingies吗? - 我听说有一种更好的碰撞方式(@ dirt.getBoundries w / intersect)
谢谢! Greets,Tim。
答案 0 :(得分:2)
对你的评论提出了一个很好的观点。快速查看它似乎不是你创建lvl变量 - 除非它通过DI完成?
但是堆栈跟踪通常会为您提供确切的问题。