我一直在尋找所有其他問題,但我似乎無法找到我的問題的答案:我需要使用非靜態字段(這是每個對象我分開我創建),我需要在另一個類中訪問該變量。代碼塊:(Enemytank.java)在另一個類中使用對象特定的變量
public class Enemytank extends MoveableGameItem implements IStepListener, IAlarmListener
{
private Battlefield mygame;
private Enemytank enemyTank1;
private static int enemyWaveSize = 1;
private static int remainingKills = enemyWaveSize;
private static double startupEnemyHealth = 100.00;
private double enemyHealth = startupEnemyHealth;
public int enemyStage = 0;
/**
* Constructor.
*/
public Enemytank(Battlefield mg)
{
mygame = mg;
setImage("/images/enemytank.png", 27, 33);
setPosition(25, 35);
// snelheid 5, naar rechts
setDirectionSpeed(0, 4);
startMoving();
mygame.addStepListener(this);
}
}
和類中,我需要的變量enemyStage:(Playertank.java)
public void collisionOccured(GameItem collidedItem)
{
Enemytank enemyTank1 = new Enemytank(mygame);
System.out.println("pt= " + enemyTank1.getEnemyStage());
if ((collidedItem instanceof Enemytank) && (playerShields > 0) && (enemyTank1.getEnemyStage() != 3))
{
this.playerShields--;
mygame.setShieldsonDashboard(playerShields);
}
else if ((collidedItem instanceof Enemytank) && (playerShields <= 0) && (enemyTank1.getEnemyStage() != 3))
{
System.out.println("hit player!");
this.playerLives--;
mygame.setLivesonDashboard(playerLives);
}
if (collidedItem instanceof Shield)
{
this.playerShields++;
mygame.setShieldsonDashboard(playerShields);
mygame.deleteGameItem(collidedItem);
this.playerUpgrades();
}
}
我現在從enemyTank1獲得的價值。 getEnemyStage()是0,因爲我相信我用Enemytank enemyTank1 = new Enemytank(mygame)創造了一個新對象;。我怎樣才能引用現有的對象,並獲得具體的價值? enemyStage用於檢查怪物是否已經死亡或幾乎死亡。
提前THX :) 火焰
你是什麼意思?「我如何引用現有對象,獲得具體的價值?「。你感興趣的現有對象是什麼?您可能想要顯示更多的代碼。 enemyStage如何以及在哪裏設置? – Raam 2012-04-20 11:26:08