这是我第一次在这里发帖,对不起,如果我有任何错误。 :/

好的,我之前在Java领域工作过BIT,直到现在才真正尝试制作游戏。
我试图用Walls制作一个简单的平台游戏,但它们使我很难。
我现在有一个简单的代码设置:

人们想要堆栈跟踪..(旧的我解决了这个问题,但是我得到了新的错误,请进一步查看)

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) {


}

}


班级:

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中制作多个Dirttyies?
-我听说有一种更好的方法来进行碰撞(@ w。相交的dirty.getBoundries)

谢谢!
问候,蒂姆。

最佳答案

对您问题的评论很有意义。快速浏览一下,看起来好像没有创建lvl变量-除非它是通过DI完成的?

但是堆栈跟踪通常会为您提供出现问题的确切行。

10-08 14:01