我怎样才能在我的游戏中向下移动我的自定义按钮

how can i move my custom button down on my game

所以我正在关注如何制作 2D 游戏的 tutorial 而我的按钮没有,想去我指定的地方我要把我的代码放在一个单独的网站上因为它与我制作的其他 classes 相关联。但这是我正在查看的 class:MenuState:

package game.dl.gamestates;

import java.awt.Graphics2D;

import game.dl.Managers.MouseManager;
import game.dl.gamestate.GameState;
import game.dl.gamestate.GameStateManager;
import game.dl.gamestate.gameStateButton;
import game.dl.main.Main;

public class MenuState extends GameState {

    MouseManager mm;
    gameStateButton startGame;

    public MenuState(GameStateManager gsm) {
        super(gsm);

    }

    @Override
    public void init() {

        startGame = new gameStateButton(Main.width / 2, 200, new DungeonLvlLoader(gsm), gsm, "start Game");
        mm = new MouseManager();

    }

    @Override
    public void tick(double deltaTime) {
        mm.tick();
        startGame.tick();
    }

    @Override
    public void render(Graphics2D g) {
        startGame.render(g);
        mm.render(g);
//      g.drawString("TESTING", Main.width, Main.height);
//      g.drawString("Hello World!", 150, 200);
        g.clipRect(0, 0, Main.width, Main.height);

    }

}

这是 GameStateButton.java class:

package game.dl.gamestate;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import game.dl.Managers.MouseManager;
import game.dl.main.Assets;
import game.gos.main.VectorToF;

public class gameStateButton extends Rectangle{

    private VectorToF pos = new VectorToF();
    private GameState gameState;
    private GameStateManager gsm;
//  private boolean isClicked;
    private boolean isHeldOver;
    private int width = 32 *3;
    private int height = 32;
    private BufferedImage defaultImage;
    private String buttonMsg;


    public gameStateButton(float xpos, float ypos, GameState gameState, GameStateManager gsm, String buttonMsg) {
        this.gameState = gameState;
        this.gsm = gsm;
        this.pos.xpos = pos.xpos;
        this.pos.ypos = pos.ypos;
        this.buttonMsg = buttonMsg;
        setBounds((int)pos.xpos, (int)pos.ypos, width, height);
        defaultImage = Assets.getButton_notHoveredOver();
    }

    public gameStateButton(float xpos, float ypos, String buttonMsg) {
        this.pos.xpos = pos.xpos;
        this.pos.ypos = pos.ypos;
        this.buttonMsg = buttonMsg;
        setBounds((int)pos.xpos, (int)pos.ypos, width, height);
        defaultImage = Assets.getButton_notHoveredOver();
    }

    public void tick(){
        setBounds((int)pos.xpos, (int)pos.ypos, width, height);

        if(getBounds().contains(MouseManager.mouse)){
            isHeldOver = true;
        }else{
            isHeldOver = false;
        }

        if(isHeldOver){
            if(defaultImage != Assets.getButton_hoveredOver()){
                defaultImage = Assets.getButton_hoveredOver();
            }

        }else{
            if(defaultImage != Assets.getButton_notHoveredOver()){
                defaultImage = Assets.getButton_notHoveredOver();
            }
        }

        if(gameState != null){
            if(isHeldOver){
                if(isPressed()){
                    gsm.states.push(gameState);
                    isHeldOver = false;
                    MouseManager.pressed = false;
                }
            }
        }


    }

    public void render(Graphics2D g){
        g.drawImage(defaultImage, (int)pos.xpos, (int)pos.ypos, width, height, null);
        g.drawString(buttonMsg, pos.xpos, pos.ypos);
    }

//  public boolean isClicked(){
//      return isClicked;
//  }
    public boolean isHeldOver(){
        return isHeldOver;
    }

    public boolean isPressed (){
        return MouseManager.pressed;
    }

}

这是完整的代码转储: https://drive.google.com/file/d/0B7JJSxzNdpBrNmdFRTgyVXhRSVU/view?usp=sharing

您的 gameStateButton class 初始化为:

startGame = new gameStateButton(Main.width / 2, 200, new DungeonLvlLoader(gsm), gsm, "start Game");

查看您的代码 zip(顺便说一下,下次在此处添加 gameStateButton.java 代码),按钮的固定宽度为 32*3 像素,高度为 32 像素。

您将其设置为 x = Main。width/2,y=200。

您可以将这些值更改为您想要的任何值。

现在您还没有告诉我们实际结果是什么(即它现在显示的位置),也没有告诉我们预期结果是什么(即您希望它出现的位置),但是那条线定义了它的初始位置: 如果它没有显示在那里,那么某些东西正在改变它的位置。如果它根本不显示,那么可能是您以错误的顺序绘制它并且它被覆盖了。

因此,在玩弄并查看我的其他 class 一段时间后,我发现了问题所在。在 gameStateButton.java 中,构造函数从 class 字段而不是构造函数变量中提取 x 和 y 位置,因此它应该如下所示:

public gameStateButton(float xpos, float ypos, GameState gameState,     GameStateManager gsm, String buttonMsg) {
    this.gameState = gameState;
    this.gsm = gsm;
    this.pos.xpos = xpos;
    this.pos.ypos = ypos;
    this.buttonMsg = buttonMsg;
    setBounds((int)pos.xpos, (int)pos.ypos, width, height);
    defaultImage = Assets.getButton_notHoveredOver();
}

不是这个:

public gameStateButton(float xpos, float ypos, GameState gameState, GameStateManager gsm, String buttonMsg) {
    this.gameState = gameState;
    this.gsm = gsm;
    this.pos.xpos = pos.xpos;
    this.pos.ypos = pos.ypos;
    this.buttonMsg = buttonMsg;
    setBounds((int)pos.xpos, (int)pos.ypos, width, height);
    defaultImage = Assets.getButton_notHoveredOver();
}