(我是java新手,所以请不要让你的答案太混乱!!!!:P。)我刚刚使用eclipse创建了一个游戏,我真的很想制作一个有趣的游戏!我查看了一些帮助我创建它的教程。问题是我不能在游戏中移动。我真的很感激帮助!这是我在游戏中尝试使用 wasd 移动时收到的错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at StickFireBasic.KeyAdapt.keyReleased(KeyAdapt.java:19)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(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.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$000(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)
My class files are: Main, GameFrame, Entity, Player , KeyAdapt.
主要的:
package StickFireBasic;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("StickFire");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(true);
frame.add(new GameFrame());
frame.setVisible(true);
}
}
游戏框架:
package StickFireBasic;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GameFrame extends JPanel implements ActionListener{
Timer mainTimer;
Player player;
public GameFrame() {
setFocusable(true);
player = new Player(400, 300);
addKeyListener(new KeyAdapt(player));
mainTimer = new Timer(10, this);
mainTimer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
player.draw(g2d);
}
@Override
public void actionPerformed(ActionEvent arg0) {
player.update();
repaint();
}
}
实体:
package StickFireBasic;
import java.awt.Graphics2D;
public class Entity {
int x, y;
public Entity(int x, int y) {
this.x = x;
this.y = y;
}
public void update() {
}
public void draw(Graphics2D g2d) {
}
}
玩家:
package StickFireBasic;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Player extends Entity{
int velX = 0, velY = 0;
public Player(int x, int y) {
super(x, y);
}
public void update() {
y += velY;
x += velX;
}
public void draw(Graphics2D g2d) {
g2d.drawImage(getPlayerImg(), x, y, null);
}
public Image getPlayerImg() {
ImageIcon ic = new ImageIcon("C:/img/stickguy.png/");
return ic.getImage();
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
velY = -2;
} else if (key == KeyEvent.VK_S) {
velY = 2;
} else if (key == KeyEvent.VK_A) {
velX = -2;
} else if (key == KeyEvent.VK_D) {
velX = 2;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_W) {
velY = 0;
} else if (key == KeyEvent.VK_S) {
velY = 0;
} else if (key == KeyEvent.VK_A) {
velX = 0;
} else if (key == KeyEvent.VK_D) {
velX = 0;
}
}
}
键适配:
package StickFireBasic;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyAdapt extends KeyAdapter {
Player p;
public KeyAdapt(Player player) {
}
public void keyPressed(KeyEvent e) {
p.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
p.keyReleased(e);
}
}