1

我正在使用 acm 库(acm.jar),这是斯坦福学生在他们的 Java 课程 CS106A 中使用的同一个库,可以更轻松地创建图形应用程序。

acm.jar 可以在这里找到:http: //jtf.acm.org/

以下代码将字符精灵添加到图形窗口。如果我按 z ,角色会通过使用 Java 线程来动画自己移动他的弓和箭开始垂直移动。到目前为止没有错误。

现在我希望能够在线程上执行碰撞检测GImage object(linkArrow)。当我尝试这样做时,我的程序中发生了错误:

arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY());
arrowObject = getElementAt(arrowPoint);

我收到此错误:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException

您可以 ctrl+f 并键入 bug 以查看我的程序的错误位置。

在使用我的游戏“BreakOut”使用图形矩形和椭圆之前,我已经完成了碰撞检测,GPoint并且getElementAt从未遇到过使用GPointand的问题getElementAt

这是我的代码:第一类是运行线程的主程序。第二类是线程。

        import java.awt.event.KeyEvent;
        import acm.graphics.GImage;
        import acm.program.GraphicsProgram;
        import acm.util.RandomGenerator;

        public class Link extends GraphicsProgram {


            private static final double GRAVITY = 1;
            public void init(){
                setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
                addLink();

                addKeyListeners();
        }


            private void addLink(){
                linkCharacter = new GImage("link sprites/linkFaceRight/link_frame_1_face_right.png");
                add(linkCharacter,link_Location_XCoord,link_Location_YCoord);
            }

          public void keyPressed(KeyEvent e){ 
                /* Link's Movement
                 * 
                 */
              char linkMoveRightKey = e.getKeyChar();
            if(linkMoveRightKey == 'z')
                    {

                    xSpeed = 0;
                    ySpeed = 0;
                    pause(arrowDELAY);
                    linkCharacter.move(xSpeed, ySpeed);
                    linkCharacter.setImage(linkAttackWithBow[linkFrame]);

                    linkFrame++;
                    callArrow();

 // BUG 
           arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY());
           arrowObject = getElementAt(arrowPoint);

           }
  //                  


                   /*
                    * summon link's arrow
                    */
                    }

                    if(linkFrame>=linkAttackWithBow.length){
                        linkFrame = 0;
                    }


            }



            private void callArrow(){
                if(linkFrame == 2){
                 linkArrow = new ArrowThread(SIZE, rgen.nextColor());
                add(linkArrow,linkCharacter.getX(),linkCharacter.getY());

                Thread arrowThread = new Thread(linkArrow);
                arrowThread.start();




                }
            }

        private ArrowThread linkArrow;
            private int SIZE = 400;

            private RandomGenerator rgen = RandomGenerator.getInstance();
            private GImage gameBackgroundImage;

            private GImage linkCharacter;

            private double arrowDELAY = 28;


            private  int link_Location_XCoord = 50;
            private  int link_Location_YCoord = 50 ;
            private final int APPLICATION_WIDTH = 1200;
            private final int APPLICATION_HEIGHT = 800;
         private String[] linkAttackWithBow = {"link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png"};

            private int linkFrame = 0;
            private int xSpeed =5; //the number of pixels to move in x
            private int ySpeed = 0; //0 so you only move horizontally
        }

这是我的 Thread 类来产生箭头

import java.awt.Color;

import acm.graphics.GImage;


public class ArrowThread extends GImage implements Runnable{

    public ArrowThread(int SIZE,Color color){
        super("link sprites/linkAttackWithBow/arrow.png", SIZE,SIZE);


    }

    public void run(){
        for(int i = 0; i < 1000/STEP;i++){
            pause(60);

            move(arrowSpeedX,arrowSpeedY);

        }

    }






    private static final int arrowSpeedX = 0;
    private static final int arrowSpeedY = -5;

    private static final double STEP  = 5; 


}
4

1 回答 1

1

看起来问题是该成员linkArrow在第一帧上为空。查看检查:

if(linkFrame == 2){
    linkArrow = new ArrowThread(SIZE, rgen.nextColor());
于 2012-06-07T06:40:00.283 回答