3

so for the game I'm creating I have a few classes that extend GameDriver.

Up until this point, on all the other classes I've been able to extend GameDriver and then In GameDriver I can do:

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

Today I started on the GameAI class and I extended GameDriver, and when I put:

GameAI Enemy = new GameAI();

In the same spot I put the other line of code (Right below public class GameDriver)

I get:

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)

If I put it in the public void init() of my applet, then it doesn't give an error on run, but then I wouldn't be able to access it from my other methods, am I over looking something? All nighters normally don't help my brain...

This is what the GameAI looks like as of right now:

public class GameAI extends GameDriver {

    public int life;
    public int energy;

    public void drawPhase(){

    }

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

And then some bits of the 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 に渡します。

つまり、

class GameAI {
   private GameDriver gameDriver;

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

   //.... more code
}  

編集 2
GameAI オブジェクトが 1 つ必要な場合は、

GameAI gameAi = new GameAI(this);

それらの配列またはリストが必要な場合は、これをループで実行します。

于 2012-08-19T15:19:41.237 に答える