Digger游戏中的敌人需要自行移动,代码破坏,我不知道为什么

时间:2015-02-13 23:14:29

标签: javascript multithreading compiler-errors artificial-intelligence

在我的csse课程中,我们正在制作一个Digger游戏http://www.futrega.org/digger/,到目前为止我们已经完成了游戏的这些功能 •从文件加载级别 •英雄可以拿起祖母绿获得积分并移动 •使用U和D在加载的级别之间切换。 现在我正在努力让Hobbin独自移动(它是通过污垢和其他物品吃的敌人)朝向英雄并改变英雄的生命,这是我迄今为止的移动功能代码。

public void move(){
    int Hero_x = Level.hero.myLocation.getX();
    int Hero_y = Level.hero.myLocation.getY();
    int X = this.myLocation.getX();
    int Y = this.myLocation.getY();
    int speed = 2;
    int x_distance =  Hero_x - X;
    int y_distance = Hero_y - Y;
    double distance = Math.hypot(x_distance, y_distance);
    this.x_velocity = (int) ((speed/distance) * x_distance);
    this.y_velocity = (int) ((speed/distance) * y_distance);
    while(Level.map[X + this.x_velocity][Y + this.y_velocity] instanceof Hero == false) {
        Level.map[X][Y] = new Tunnel(new Coordinate(X, Y));
        if ((X + this.x_velocity) < 0 || (X + this.x_velocity) > this.MAP_WIDTH - 1) {
            this.myLocation.setCoordinate(X, Y);
            Level.map[X][Y] = this;
        } else if ((Y + this.y_velocity) < 0 
                     || (Y + this.y_velocity) > this.MAP_HEIGHT - 1) {
            this.myLocation.setCoordinate(X, Y);
            Level.map[X][Y] = this;
        } else {
            this.myLocation.setCoordinate(X + this.x_velocity, Y + this.y_velocity);
            Level.map[X + this.x_velocity][Y + this.y_velocity] = this;
        }
    }
    if(World.lives == 0) {
        new Main().launch();
    }
    World.lives = World.lives - 1;
    Level.hero.myLocation.setCoordinate(Level.hero.myLocation.getX(),
                                             Level.hero.myLocation.getY());
    Level.map[Level.hero.myLocation.getX()][Level.hero.myLocation.getY()] = Level.hero;
    this.myLocation.setCoordinate(this.Location.getX(), this.Location.getY());
    Level.map[this.myLocation.getX()][this.myLocation.getY()] = this;
}

基本上它的作用是它获得X和Y的速度(我在谷歌搜索,这是关于如何让敌人跟随玩家的答案,但我不知道这是否正确)然后它检查敌人的下一个位置是否是Hero的一个实例,如果不是,那么我实现的代码只是让它移动,如果它超过了屏幕的边界,那么它就不会移动。然后如果while语句被破坏(Hobbin的下一个位置是Hero的一个实例)那么它会检查生命是否为0,如果它确实然后它重新开始,但如果它没有,那么它会减去生命并且它将英雄和滚刀放回原来的位置,保持木板上的污垢和其他一切尽可能相同。

new Thread(hobbin).start();

在关卡的构造函数中,我认为应该在创建新关卡时使hobbin正确运行。 但是,当我运行代码时,我收到此错误,

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Hobbin.draw(Hobbin.java:71)
    at Level.paintComponent(Level.java:82)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1100(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.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)

有人可以解释一下吗?如果我的代码错了,有人可以解释一下吗?感谢。

1 个答案:

答案 0 :(得分:1)

这可能会有所帮助:

我注意到您担心您的AI代码会破坏您的程序,但在错误报告中看起来并非如此。请注意,在游戏的图形调用阶段,paintComponent调用期间,您正在接收的空指针异常位于hobbin.draw函数中。

赔率是实际错误,是一个相对路径字符串,如&#34; /mycoolpictures/hobbinframespritesheet.png"错误引用或错误拼写...以便您的图像加载器从文件未找到错误中获取空图片,但尝试将其传递到您的游戏中...然后是滚刀的DOOOOM。

希望这有帮助。