1

我一直在查看所有其他问题,但似乎找不到问题的答案:我需要使用非静态字段(对于我创建的每个对象都是单独的),并且我需要在另一个中访问该变量班级。一段代码:(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 用于检查怪物是死了还是快死了。

提前谢谢 :) 火焰

4

2 回答 2

0

您需要利用多态性。getEnemyStage在类中声明方法GameItem(或者它是接口?),然后您将能够collidedItem.getEnemyStage()从该对象中询问并获取数据。或者,向下转换collidedItem为并EnemyTank调用该方法:((EnemyTank)collidedItem).getEnemyStage()

另外, makeenemyStage private不需要这样public,因为它将通过调用 getter 方法间接访问。

if (collidedItem instanceof Enemytank && playerShields > 0 && 
    ((Enemytank)collidedItem).getEnemyStage() != 3) {
  ....
}
于 2012-04-20T11:27:43.383 回答
0

当您调用碰撞发生时,您将引用传递给对象。因此,将碰撞发生更改为:

public void collisionOccured(GameItem collidedItem, Enemytank enemyTank) //a change here
    {
        System.out.println("pt= " + enemyTank.getEnemyStage()); // now you can call enemyTank with the correct refrence
        if ((collidedItem instanceof Enemytank) && (playerShields > 0) && (enemyTank1.getEnemyStage() != 3))
        {
            this.playerShields--;
            mygame.setShieldsonDashboard(playerShields);

        }
        else if ((collidedItem instanceof Enemytank) && (playerShields <= 0) && (enemyTank.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();
        }
    }

而且,当您从 EnemyTank 类中调用 collisionOccured 时,您会喜欢这样:

collisionOccured(collidedItem, this); //where this refrence to this object (which is a EnemyTank)

有了这个答案,我假设您从 EnemyTank 类中调用了 collisonOccured,因为您没有发布该调用的位置。如果不是,则在调用 collisionOccured 时需要对 EnemyTank 的对象进行引用(因此this在调用中更改对正确对象的引用)

于 2012-04-20T11:30:52.737 回答