我试图写一个地牢大师游戏,英雄会经过不同的房间,与怪物战斗并获得物品。我想要save the hero every time they get to a new level
,但即使我对英雄以及英雄使用的所有内容都有IOException
,我也会获得implemented Serializable
。一些帮助将非常感激。谢谢。
具有保存方法的面板:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
* The panel where the game happens.
*/
public class Panel extends JPanel implements Runnable, KeyListener {
private Thread t;
private Hero hero;
private File f;
...
public Panel(String n, int g) {
f = new File("save.dat");
...
}
public void save() {
try {
JOptionPane.showMessageDialog(null, "Saving...");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(hero);
out.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error saving file.");
}
}
英雄课程:
import java.awt.Point;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
/**
* Our awesome hero.
*/
public class Hero extends Character implements Serializable {
/** Array list of items of hero */
private ArrayList<Item> items;
/** Character's location */
private Point location;
private int dx, dy, tempX, tempY;
/**
* Constructor
*
* @param n
* Hero's name.
* @param q
* Hero's catch phrase.
* @param start
* Hero's starting point.
*/
public Hero(String n, String q, Point start) {
super(n, q, 15, 1, 0);
location = start;
items = new ArrayList<Item>();
tempX = (int) location.getX();
tempY = (int) location.getY();
}
/**
* Drawing the hero.
*
* @param g The graphics
*/
public void drawHero(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect((int) location.getX(), (int) location.getY(), 50, 50);
move();
}
/**
* Moving the hero.
*/
public void move() {
setX(getX() + dx);
setY(getY() + dy);
}
/**
* Stopping the hero.
*/
public void stop() {
dx = 0;
dy = 0;
}
/**
* Attacking the enemy.
*
* @param C The enemy that will be attacked.
*/
public void attack(Character C) {
int h = (int) (Math.random() * 3) + 1;
h += getLevel();
C.takeDamage(h);
}
/**
* Get the items.
*
* @return The item arraylist.
*/
public ArrayList<Item> getItems() {
return items;
}
/**
* Picking up the item and check if
* inventory is full.
*/
public boolean pickUpItem(Item i) {
if (items.size() == 5)
return false;
else {
items.add(i);
return true;
}
}
/**
* Check what room is the hero in.
*/
public char checkRoom(Level l) {
int x = (int) location.getX();
int y = (int) location.getY();
if ((x < 400 && y < 400 && x >= 0 && y >= 0)) {
return l.getRoom(location);
} else
return 'n';
}
/**
* Remove item by its name.
*
* @param i The item to be removed.
*/
public void removeItem(Item i) {
int item = items.indexOf(i);
items.remove(item);
}
/**
* Remove item by its index.
*
* @param index The index of the item to be removed.
*/
public void removeItem(int index) {
index -= 1;
Item i = items.get(index);
int gold = i.getValue();
collectGold(gold);
items.remove(index);
}
/**
* Getting hero's location.
*
* @return The hero's location.
*/
public Point getLocation() {
return location;
}
/**
* Setting the hero's location.
*
* @param p The location that will be set to.
*/
public void setLocation(Point p) {
int x = (int) p.getX();
int y = (int) p.getY();
location.setLocation(x, y);
}
/**
* Checking to see if hero is out of bounds.
*/
public void checkBounds() {
if ( getX() <= 0 || getX()+50 >= 400 || getY() <= 0 || getY()+50 >= 400 ) {
dx = 0;
dy = 0;
}
tempX = getX();
tempY = getY();
}
/**
* Stopping the hero.
*/
public void staph() {
dx = 0;
dy = 0;
setX(tempX);
setY(tempY);
}
/**
* Going north.
*/
public void goNorth() {
if ( getY() > 0 ) {
dx = 0;
dy = -2;
}
}
/**
* Going south.
*/
public void goSouth() {
if ( getY()+50 < 400 ) {
dx = 0;
dy = 2;
}
}
/**
* Going east.
*/
public void goEast() {
if ( getX()+50 < 400 ) {
dx = 2;
dy = 0;
}
}
/**
* Going west.
*/
public void goWest() {
if ( getX() > 0 ) {
dx = -2;
dy = 0;
}
}
/**
* Running north.
*/
public void runNorth() {
setY(getY() - 100);
}
/**
* Running north.
*/
public void runSouth() {
setY(getY() + 100);
}
/**
* Running north.
*/
public void runEast() {
setX(getX() + 100);
}
/**
* Running north.
*/
public void runWest() {
setX(getX() - 100);
}
/**
* Getting x location of hero.
*
* @return Hero's x location.
*/
public int getX() {
return (int) location.getX();
}
/**
* Getting y location of hero.
*
* @return Hero's y location.
*/
public int getY() {
return (int) location.getY();
}
/**
* Setting x location of hero.
*/
public void setX(double x) {
location.setLocation(x, getY());
}
/**
* Setting y location of hero.
*/
public void setY(double y) {
location.setLocation(getX(), y);
}
/**
* Check if hero is active.
*/
public boolean isActive() {
return getHP() > 0;
}
}
项目类:
import java.io.Serializable;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
* The item that the hero will have.
*/
public class Item implements Serializable {
/** Item's level */
private String name;
/** Item's gold value */
private int goldValue;
private BufferedImage img;
/**
* Constructor
*
* @param n
* Item's name.
* @param v
* Item's gold value.
*/
public Item(String n, int v, BufferedImage i) {
name = n;
goldValue = v;
img = i;
}
/**
* Get the name of the item.
*
* @return the name of the item.
*/
public String getName() {
return name;
}
/**
* Get the value of the item.
*
* @return The gold value of the item.
*/
public int getValue() {
return goldValue;
}
public BufferedImage getImage() {
return img;
}
}
答案 0 :(得分:0)
您需要将变量img
更改为transient
private transient BufferedImage img;
这可以防止异常。尽管如此,由于瞬态意味着你不能保存图像,所以不要写这个字段。我想说,你需要序列化BufferedImage的字节,可以是bytes []或类似的可验证类型。
transient关键字是here explained。它说
4)java中的瞬态变量在对象时不会被持久化或保存 被序列化。
了解详情:http://javarevisited.blogspot.com/2011/09/transient-keyword-variable-in-java.html#ixzz3ZeGZZQHd