简体   繁体   中英

Getters and Setters / Accessing variables from other classes

I have two classes Player and menuState, depending on a variable in the player class named "model" the model of the player changes. Now I want to change the model via a button in the menu but I cant figure out how to acces the "model" variable from the menuState without creating a new Player object.

My Player class:

package entitys;

import State.State;
import data.Handler;
import grafik.Animation;
import grafik.Assets;


import java.awt.*;

public class Player extends Creature {

private Animation playerWalkLeft;
private Animation playerWalkRight;
private Animation playerIdle;
private int model = 1;
public Player(Handler handler, float x, float y) {
    super(handler, x, y, Creature.DEFAULT_CREATURE_WIDTH, 112);

    bounds.x = 6;
    bounds.y = 52;
    bounds.width = 52;
    bounds.height = 64;


    if (model == 1) {
        playerWalkLeft = new Animation(6,Assets.fairy_run[0],Assets.fairy_run[1],Assets.fairy_run[2],Assets.fairy_run[3]);
        playerWalkRight = new Animation(6,Assets.fairy_run[0],Assets.fairy_run[1],Assets.fairy_run[2],Assets.fairy_run[3]);
        playerIdle = new Animation(10,Assets.fairy_idle[0],Assets.fairy_idle[1],Assets.fairy_idle[2],Assets.fairy_idle[3]);
    }else if (model == 2) {
        playerWalkLeft = new Animation(6, Assets.dragon_run[0], Assets.dragon_run[1], Assets.dragon_run[2], Assets.dragon_run[3]);
        playerWalkRight = new Animation(6, Assets.dragon_run[0], Assets.dragon_run[1], Assets.dragon_run[2], Assets.dragon_run[3]);
        playerIdle = new Animation(10, Assets.dragon_idle[0], Assets.dragon_idle[1], Assets.dragon_idle[2], Assets.dragon_idle[3]);
    }else if (model == 3) {
        playerWalkLeft = new Animation(6, Assets.wizzard_run[0], Assets.wizzard_run[1], Assets.wizzard_run[2], Assets.wizzard_run[3]);
        playerWalkRight = new Animation(6, Assets.wizzard_run[0], Assets.wizzard_run[1], Assets.wizzard_run[2], Assets.wizzard_run[3]);
        playerIdle = new Animation(10, Assets.wizzard_idle[0], Assets.wizzard_idle[1], Assets.wizzard_idle[2], Assets.wizzard_idle[3]);
    } else if (model == 4){
        playerWalkLeft = new Animation(6, Assets.knight_run[0], Assets.knight_run[1], Assets.knight_run[2], Assets.knight_run[3]);
        playerWalkRight = new Animation(6, Assets.knight_run[0], Assets.knight_run[1], Assets.knight_run[2], Assets.knight_run[3]);
        playerIdle = new Animation(10, Assets.knight_idle[0], Assets.knight_idle[1], Assets.knight_idle[2], Assets.knight_idle[3]);
    }

}



@Override
public void tick() {
    getInput();
    move();

    handler.getCamera().centerOnEntity(this);
    playerWalkRight.runAnimation();
    playerIdle.runAnimation();
    playerWalkLeft.runAnimation();
}

private void getInput() {
    xMove = 0;
    yMove = 0;

    if (handler.getKeyManager().up)
        yMove = -speed;
    if (handler.getKeyManager().down)
        yMove = speed;
    if (handler.getKeyManager().left)
        xMove = -speed;
    if (handler.getKeyManager().right)
        xMove = speed;
    if (handler.getKeyManager().escape)
        State.setState(handler.getGame().menuState);
}

@Override
public void render(Graphics g) {
    if(xMove >0) {
        playerWalkRight.drawAnimation(g,(int) (x - handler.getCamera().getxOffset()), (int) (y - handler.getCamera().getyOffset()), width, 112);
    }
    else if(xMove<0){
        playerWalkLeft.drawAnimation(g,(int) (x - handler.getCamera().getxOffset()), (int) (y - handler.getCamera().getyOffset()), width, 112);

    }
    else {
        playerIdle.drawAnimation(g,(int) (x - handler.getCamera().getxOffset()), (int) (y - handler.getCamera().getyOffset()), width, 112);

    }
    if (handler.getKeyManager().debug) {
        g.setColor(Color.red);
        g.drawRect((int) (x + bounds.x - handler.getCamera().getxOffset()),
                (int) (y + bounds.y - handler.getCamera().getyOffset()),
                bounds.width, bounds.height);
    }
}

public int getModel() {
    return model;
}

public void setModel(int model) {
    this.model = model;
}

}

My menuState class:

package State;

import data.Handler;
import grafik.Assets;
import ui.ClickListener;
import ui.UIImageButton;
import ui.UIManager;
import entitys.Player;

import java.awt.*;

public class MenuState extends State {

private UIManager uiManager;

public MenuState(Handler handler) {
    super(handler);
    uiManager = new UIManager(handler);
    handler.getMouseManager().setUiManager(uiManager);



    uiManager.addObject(new UIImageButton(144, 286, 64, 128, Assets.btn_dragon, new ClickListener() {
    @Override
    public void onClick(){
        State.setState(handler.getGame().gameState);
        //p.setModel(2);
    }
    }));

    uiManager.addObject(new UIImageButton(464, 286, 64, 128, Assets.btn_wizzard, new ClickListener() {
        @Override
        public void onClick(){
            State.setState(handler.getGame().gameState);
            //p.setModel(3);
        }
    }));

    uiManager.addObject(new UIImageButton(784, 286, 64, 128, Assets.btn_knight, new ClickListener() {
        @Override
        public void onClick(){
            State.setState(handler.getGame().gameState);
            //p.setModel(4);
        }
    }));
    uiManager.addObject(new UIImageButton(1104, 286, 64, 128, Assets.btn_fairy, new ClickListener() {
        @Override
        public void onClick(){
            State.setState(handler.getGame().gameState);
            //p.setModel(1);

        }
    }));


}


@Override
public void tick() {
    uiManager.tick();
}

@Override
public void render(Graphics g) {
    uiManager.render(g);
}

}

Thanks in advance

You could make the model field in the MenuState class and change its value on click, instead of model field in Player class if you don't want it linked to a specific player instance, that is the simplest solution. Let me know if this helped.

If the model Integer is not bound to a specific player object, then just change it into a static variable. If not, you need the Player Object by creating it or saving it in something like a Map.

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