簡體   English   中英

Getter 和 Setter / 從其他類訪問變量

[英]Getters and Setters / Accessing variables from other classes

我有兩個類 Player 和 menuState,這取決於播放器中的變量 class 命名為“模型”,播放器的 model 發生變化。 現在我想通過菜單中的按鈕更改 model 但我無法弄清楚如何在不創建新播放器 object 的情況下從 menuState 訪問“模型”變量。

我的播放器 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;
}

}

我的菜單狀態 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);
}

}

提前致謝

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. 讓我知道這是否有幫助。

如果 model Integer 未綁定到特定播放器 object,那么只需將其更改為 ZA81259CEF856559C297ZD1 變量。 如果沒有,您需要播放器 Object,方法是創建它或將其保存在 Map 之類的東西中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM