Det her er et stacktrace ikk?
Exception in thread "main" java.lang.NullPointerException
at tankWars.Actor.paint(Actor.java:37)
at tankWars.TankWars.paintWorld(TankWars.java:71)
at tankWars.TankWars.game(TankWars.java:78)
at tankWars.TankWars.main(TankWars.java:92)
Vis jeg splitter spriteCache.getSprite(spriteName) op i flere linjer således:
SpriteCache.
getSprite(
spriteName);
er det den anden linje den siger fejlen er i, men jeg ved ikke om det har nogen betydnign.
spriteCache bliver først oprettet i klassen TankWars constructor:
spriteCache = new SpriteCache();
der bliver oprettet en instans of TankWars i main metoden. Main metoden ligger også i TankWars.
TankWars game = new TankWars();
i Actor objectet hvor metoden public void paint(Graphics2D) ligger bliver spriteCache initaliseret således:
public Actor(TankWars tw) {
game = tw;
spriteCache = game.getSpriteCache();
frameSpeed = 1;
currentFrame = 1;
spriteName = "tankSheet.gif";
}
TankWars.getSpriteCache() ser sådan her ud:
public SpriteCache getSpriteCache() {return spriteCache;}
Så for mig ser det ud som om spriteCache indeholder et SpriteCache objekt...
her er hele koden til spillet (fylder ikke så meget endu):
//TankWars
package tankWars;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import game.KeyboardInput;
public class TankWars extends Canvas {
public static final int PLAY_HEIGHT = 400;
public static final int HEIGHT = 440;
public static final int WIDTH = 600;
public static final int SPEED = 10;
private boolean done;
private KeyboardInput keyboard;
private Vector actors;
private BufferStrategy strategy;
private SpriteCache spriteCache;
private Actor a1;
public TankWars() {
a1 = new Actor(this);
a1.setXPos(3);
a1.setYPos(3);
a1.setSpriteName("tankSheet.gif");
a1.setWidth(24);
a1.setHeight(32);
setBounds(0, 0, TankWars.WIDTH, TankWars.HEIGHT);
JFrame vindue = new JFrame();
vindue.setTitle("Tank Wars");
vindue.setBounds(0, 0, TankWars.WIDTH, TankWars.HEIGHT);
vindue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
vindue.setResizable(false);
JPanel panel = (JPanel)vindue.getContentPane();
panel.setPreferredSize(new Dimension(TankWars.WIDTH, TankWars.HEIGHT));
panel.setLayout(null);
panel.add(this);
this.setBackground(new Color(0, 255, 0));
vindue.setVisible(true);
done = false;
keyboard = new KeyboardInput();
actors = new Vector();
spriteCache = new SpriteCache();
createBufferStrategy(2);
strategy = getBufferStrategy();
}
public void updateWorld() {
for(Iterator i = actors.iterator(); i.hasNext();) {
Actor a = (Actor)i.next();
a.act();
}
a1.act();
}
public void paintWorld() {
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
a1.paint(g);
strategy.show();
}
public void game() {
while(!done) {
updateWorld();
paintWorld();
try {
Thread.sleep(SPEED);
}
catch (InterruptedException e) {
System.out.println(e + "");
}
}
}
public SpriteCache getSpriteCache() {return spriteCache;}
public static void main(String[] args) {
TankWars game = new TankWars();
game.game();
}
}
//SpriteCache
package tankWars;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import javax.imageio.ImageIO;
public class SpriteCache {
private HashMap sprites;
public SpriteCache() {
sprites = new HashMap();
}
private BufferedImage loadImage(String image) {
URL url=null;
try {
url = getClass().getResource(image);
return ImageIO.read(url);
}
catch (Exception e) {
System.out.println(e);
System.exit(0);
return null;
}
}
public BufferedImage getSprite(String image) {
BufferedImage img = (BufferedImage)sprites.get(image);
if(img == null) {
img = loadImage("res\\" + image);
sprites.put(image, img);
}
return img;
}
}
//Actor
package spaceInvaders1;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
public class Actor {
protected int x, y;
protected int width, height;
protected String[] spriteNames;
protected int currentFrame;
protected Stage stage;
protected SpriteCache spriteCache;
protected int frameSpeed;
protected int t;
protected boolean markedForRemoval;
public Actor(Stage stage) {
this.stage = stage;
spriteCache = stage.getSpriteCache();
currentFrame = 0;
frameSpeed = 1;
t = 0;
}
public void act() {
t++;
if(t % frameSpeed == 0) {
currentFrame = currentFrame + 1;
if(currentFrame >= spriteNames.length) {
currentFrame = 0;
}
}
}
public void paint(Graphics2D g) {
g.drawImage(spriteCache.getSprite(spriteNames[currentFrame]), x, y, stage);
}
public void collision(Actor a) {}
public void remove() {
markedForRemoval = true;
}
public boolean isMarkedForRemoval() {
return markedForRemoval;
}
public Rectangle getBounds() {
return new Rectangle(x, y, height, width);
}
public int getX() {return x;}
public void setX(int i) {x = i;}
public int getY() {return y;}
public void setY(int i) {y = i;}
public int getFrameSpeed() {return frameSpeed;}
public void setFrameSpeed(int i) {frameSpeed = i;}
public int getWidth() {return width;}
public void setWidth(int i) {width = i;}
public int getHeight() {return height;}
public void setHeight(int i) {height = i;}
public void setSpriteNames(String[] names) {
spriteNames = names;
width = 0;
height = 0;
for(int i = 0; i < spriteNames.length; i++) {
BufferedImage image = spriteCache.getSprite(spriteNames[i]);
width = Math.max(width, image.getWidth());
height = Math.max(height, image.getHeight());
}
}
}
Indlæg senest redigeret d. 24.10.2008 19:53 af Bruger #13669