3

所以对于我正在创建的游戏,我有一些扩展 GameDriver 的类。

到目前为止,在所有其他类上我已经能够扩展 GameDriver 然后在 GameDriver 中我可以做到:

ArrayList<Card>  library = new ArrayList<Card>();

今天我开始学习 GameAI 课程并扩展了 GameDriver,当我把:

GameAI Enemy = new GameAI();

在同一个地方,我放了另一行代码(在公共类 GameDriver 的正下方)

我得到:

java.lang.StackOverflowError
at java.util.WeakHashMap.expungeStaleEntries(Unknown Source)
at java.util.WeakHashMap.getTable(Unknown Source)
at java.util.WeakHashMap.get(Unknown Source)
at java.awt.Component.checkCoalescing(Unknown Source)
at java.awt.Component.<init>(Unknown Source)
at java.awt.Container.<init>(Unknown Source)
at java.awt.Panel.<init>(Unknown Source)
at java.awt.Panel.<init>(Unknown Source)
at java.applet.Applet.<init>(Unknown Source)
at GameDriver.<init>(GameDriver.java:14)
at GameAI.<init>(GameAI.java:8)
at GameDriver.<init>(GameDriver.java:40)
at GameAI.<init>(GameAI.java:8)

如果我把它放在我的小程序的 public void init() 中,那么它在运行时不会出错,但是我将无法从我的其他方法访问它,我是不是在看东西?通宵达旦通常对我的大脑没有帮助...

这就是 GameAI 现在的样子:

public class GameAI extends GameDriver {

    public int life;
    public int energy;

    public void drawPhase(){

    }

    public GameAI(){
        life = 20;
        energy = 2;
    }
}

然后是 GameDriver 的一些内容:

public class GameDriver extends Applet implements MouseMotionListener,MouseListener {

Graphics g; 
Image offscreen;
Dimension dim; 

int playerLife = 20;
int playerEnergy = 8;

int xMouse; 
int yMouse;
int lineThickness = 4;
int handSize = 6;
int currentHover;
boolean slotHover;
int currentSelected;
boolean slotClicked;
int currentHoverBoard;
boolean slotHoverBoard;
boolean slotClickedBoard;
int currentSelectedBoard;

boolean canPlace;
ArrayList<Card>  library = new ArrayList<Card>();
ArrayList<Card>  hand = new ArrayList<Card>();
ArrayList<Card>  playerBoard = new ArrayList<Card>();
GameAI Enemy;
int[] handBoxX = new int[handSize];
int[] handBoxY = new int[handSize];
int[] handBoxW = new int[handSize];
int[] handBoxH = new int[handSize];

int[] playerBoardX = new int[8];
int[] playerBoardY = new int[8];
int[] playerBoardW = new int[8];
int[] playerBoardH = new int[8];



public void init(){
    this.setSize(640, 480);
    dim = this.getSize();
    addMouseMotionListener(this);
    addMouseListener(this);
    createLibrary();
    drawFirstHand();
    printHand();


    GameAI Enemy = new GameAI();
    checkEnemy();



    offscreen = createImage(this.getSize().width,this.getSize().height);
    g = offscreen.getGraphics(); 
}

public void checkEnemy(){
    System.out.println(Enemy.energy);
}

... Alot more methods and stuff below, but nothing to do with the GameAI enemy
4

1 回答 1

3

您正在 GameDriver 内部创建一个 GameAI 对象,它是它扩展的类。这将导致递归继续,直到内存不足。

解决方案:不要这样做。您的 GameAI 类不应扩展 GameDriver,因为这是共享信息的错误方式,即使您没有这种递归噩梦,它也根本无法工作。而是给 GameAI 一个 GameDriver 字段,并通过其构造函数将 GameDriver 实例传递给 GameAI。

IE,

class GameAI {
   private GameDriver gameDriver;

   public GameAI(GameDriver gameDriver) {
      this.gameDriver = gameDriver;
   }

   //.... more code
}  

编辑 2
如果你想要一个 GameAI 对象,你会这样做

GameAI gameAi = new GameAI(this);

如果您想要一个数组或它们的列表,您可以循环执行此操作。

于 2012-08-19T15:19:41.237 回答