简体   繁体   English

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

[英]how can i move my custom button down on my game

so I'm following a tutorial for how to make a 2D game and my button does not, want to go to the place i specify i am going to put the code i have on a separate site as its connected to other classes I've made. 因此,我正在按照有关如何制作2D游戏的教程进行操作,而我的按钮却没有,要转到我指定的位置,我要将我拥有的代码放置在单独的站点上,因为它已与其他类相连制作。 but this is the class I'm looking at: MenuState: 但这是我正在查看的类: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);

    }

}

this is the GameStateButton.java class: 这是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;
    }

}

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

Your gameStateButton class is initialized as: 您的gameStateButton类初始化为:

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

Looking inside your code zip (btw, next time add the gameStateButton.java code here), you have a fixed width of 32*3 pixel and 32 pixel height for the button. 在您的代码zip内部(顺便说一句,下一次在此处添加gameStateButton.java代码),您将看到按钮的固定宽度为32 * 3像素,高度为32像素。

You are setting it at x = Main.width/2, y=200. 您将其设置为x = Main.width / 2,y = 200。

You can change those values to be whatever you want. 您可以将这些值更改为所需的值。

Now you have not told us what is the actual result (ie where it shows now), nor what is the expected result (ie where you want it to be), but that line is what is defining its initial position: if it doesn't show there, then something is changing its place. 现在您还没有告诉我们实际结果是什么(即现在显示的位置),还是预期结果是什么(即您想要的结果),但是那一行是定义其初始位置的条件:如果没有, t在那里显示,则说明某些位置正在改变。 If it doesn't show at all, then probably you are drawing it in the wrong order and it gets overwritten. 如果根本不显示,则可能是您以错误的顺序绘制它,它被覆盖了。

So after playing around and looking at my other classes for a while I found the problem. 因此,在玩了一段时间并看了我的其他课程后,我发现了问题所在。 in gameStateButton.java the constructor was pulling the x and y pos from the class fields and not the constructor variable so it should look like this: 在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();
}

and NOT this: 不是这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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