我一直在查看所有其他问题,但似乎找不到问题的答案:我需要使用非静态字段(对于我创建的每个对象都是单独的),并且我需要在另一个中访问该变量班级。一段代码:(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,因为我相信我用EnemytankenemyTank1 = new Enemytank(mygame); 创建了一个新对象;. 如何引用现有对象并获得该特定值?enemyStage 用于检查怪物是死了还是快死了。
提前谢谢 :) 火焰