Search code examples
javagraphics2d

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. but this is the class I'm looking at: 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:

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


Solution

  • 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:

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