0

这是我所拥有的:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements ActionListener, KeyListener, ImageObserver {
        public Image ship;
        public Image enemy;
        private int xLoc;
        private int yLoc = 180;
        private int xLoc2 = 700;
        private int yLoc2 = 180;
        private int xVel;
        private int yVel;
        private int xVel_en;
        private int xVel_sh = 6;
        private ImageObserver observer = null;
        private ArrayList<Base> shield = new ArrayList<Base>();
        private Image shieldPiece;
        private Shot shot;

        public GamePanel()  {
            ship = Toolkit.getDefaultToolkit().createImage("ship3.png");
            enemy = Toolkit.getDefaultToolkit().createImage("alien-ship-1.png");
            shieldPiece = Toolkit.getDefaultToolkit().createImage("block2.png");

            for(int x = 500; x < 680; x = x + 18)
                for(int y = 60; y < 370; y = y + 18)    {
                    Base temp = new Base(x,y);
                    shield.add(temp);
                }
        }

        public void paint(Graphics g){
            super.paint(g);
            g.drawImage(ship, xLoc, yLoc, null);
            g.drawImage(enemy, xLoc2, yLoc2, null);

            for(Base b: shield)
                g.drawImage(shieldPiece, b.getShieldX(), b.getShieldY(), null);
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

            move();
            setBackground(Color.BLACK);
            repaint();

        }

        private void move() {

            int endOfWindow = getWidth();
            int endOfWindow1 = getHeight();
            if(xLoc>=(endOfWindow - ship.getWidth(null))|| xLoc<0)
                xVel = -xVel;
            xLoc +=xVel;    
            if(xLoc>=(endOfWindow - ship.getWidth(null)))
                xVel = -xVel;
            xLoc +=xVel;
            if(yLoc>=(endOfWindow1 - ship.getHeight(null)) || yLoc<0)
                yVel = -yVel;
            yLoc +=yVel;
            if(yLoc>=(endOfWindow1 - ship.getHeight(null)))
                yVel = -yVel;
            yLoc +=yVel;

        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            // TODO Auto-generated method stub

            if(arg0.getKeyCode() == KeyEvent.VK_D)
                xVel = 3;
            if(arg0.getKeyCode() == KeyEvent.VK_A)
                xVel = -3;
            if(arg0.getKeyCode() == KeyEvent.VK_W)
                yVel = -3;
            if(arg0.getKeyCode() == KeyEvent.VK_S)
                yVel = 3;

            if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
                xVel = 3;
            if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
                xVel = -3;
            if(arg0.getKeyCode() == KeyEvent.VK_UP)
                yVel = -3;
            if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
                yVel = 3;

            if(arg0.getKeyCode() == KeyEvent.VK_SPACE)  {
                Shot shoot = new Shot(xVel_sh, xLoc, yLoc);
                shoot.paint();

            }


        }

        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

            if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_A)
                xVel = 0;
            if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_S)
                yVel = 0;

            if(arg0.getKeyCode() == KeyEvent.VK_RIGHT || arg0.getKeyCode() == KeyEvent.VK_LEFT)
                xVel = 0;
            if(arg0.getKeyCode() == KeyEvent.VK_UP || arg0.getKeyCode() == KeyEvent.VK_DOWN)
                yVel = 0;

        }

        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }
}

和镜头类:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;


public class Shot{
    private int xVel_sh;
    private int xCoord;
    private int yCoord;
    private Image shot;
    private Graphics g;

    public Shot(int vel, int xLoc, int yLoc)    {
        vel = xVel_sh;
        xCoord = xLoc;
        yCoord = yLoc;
        shot = Toolkit.getDefaultToolkit().createImage("shot4.png");
    }

    public void setShotVel(int shotVel) {
        xVel_sh = shotVel;
    }

    public void paint() {
        g.drawImage(shot, xCoord, yCoord, null);
    }
}

我已经设置好了,所以当我击中空间时,它应该创建一个镜头对象,然后在镜头类中调用 paint() 方法。镜头对象将在 xLoc 和 yLoc 处创建(来自 GamePanel 类 - 这是“船”或玩家的位置)。

但是,我遇到了很多错误,从我点击空格时的 NullPointerException 开始。游戏不会崩溃,它只是不断出现空指针异常。它也跟着这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Shot.paint(Shot.java:25)
    at GamePanel.keyPressed(GamePanel.java:104)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.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)

不太清楚为什么它没有被画出来......有人可以帮忙吗?此外,我最终需要实现对象碰撞(此处未显示屏蔽类)......所以如果有人对这方面的教程有任何指导吗?:P

4

1 回答 1

3

g从未在 中初始化Shot,因此当您调用g.drawImage它时会抛出NullPointerException.

于 2013-05-04T21:33:31.067 回答