简体   繁体   中英

Using a object specific variable in another class

I've been looking across all the other questions but i cant seem to find an answer to my problem: I need to use a non-static field (which is separate for each object i create) and I need to access that variable in another class. Piece of the code: (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);
    }
}

And the class in which i need the variable 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();
        }
    }

The value I now get from enemyTank1.getEnemyStage() is 0, because i believe i make a new object with Enemytank enemyTank1 = new Enemytank(mygame); . How can I refer to the existing object, and get that specific value? enemyStage is used to check if the monster is dead or almost dead.

Thx in advance :) Flame

You need to leverage polymorphism. Declare the method getEnemyStage in the GameItem class (or is it interface?) and then you'll be able to ask collidedItem.getEnemyStage() and get the data from exactly that object. Alternatively, downcast the collidedItem into EnemyTank and call the method: ((EnemyTank)collidedItem).getEnemyStage()

Also, make enemyStage private , no need to be public as it will be accessed indirectly, by calling the getter method.

if (collidedItem instanceof Enemytank && playerShields > 0 && 
    ((Enemytank)collidedItem).getEnemyStage() != 3) {
  ....
}

You pass a refrence to the object when you call collisionOccured. so, change collisionOccured to this:

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();
        }
    }

And, when you call collisionOccured from within EnemyTank class you like this:

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

With this answer, I assume that you call collisonOccured from within the EnemyTank class as you did not post where that call is beeing made. If not, you need a refrence to a object of EnemyTank when you call collisionOccured (and therefore change this in the call to refrence to the correct object)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM