如何在游戏中移动我的自定义按钮

时间:2016-08-10 15:58:16

标签: java graphics2d

所以我跟随tutorial关于如何制作2D游戏而我的按钮没有,想要去我指定的地方我将把我的代码放在一个单独的网站上因为它与我所做的其他课程有关。但这是我正在看的课程: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类:

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

2 个答案:

答案 0 :(得分:1)

您的gameStateButton类初始化为:

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。

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

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

答案 1 :(得分:0)

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

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