Slick button will not work

若如初见. 提交于 2019-12-13 04:29:39

问题


I am following a video tutorial on a java game ( so I can go off from there ) but one of my buttons will not work (exit button). Please Help. I'm using lwjgl and slick.

main class:

package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Game extends StateBasedGame {

public static final String gamename = "Java Game Alpha 1.0";

public static final int menu = 0;
public static final int play = 1;

public Game(String gamename) {
    super(gamename);

    this.addState(new Menu(menu));
    this.addState(new Play(play));


}

public void initStatesList(GameContainer gc) throws SlickException{

    this.getState(menu).init(gc, this);
    this.getState(play).init(gc, this);
    this.enterState(menu);


}

public static void main(String[] args) {

    AppGameContainer appgc;

    try{

        appgc = new AppGameContainer(new Game(gamename));

        appgc.setDisplayMode(1600, 800, false);

        appgc.start();

    }catch(SlickException e) {

        e.printStackTrace();

    }



}

}

menu class:

package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;

public class Menu implements GameState {

Image PlayNow;
Image exitGame;

public Menu(int state){



}

@Override
public void mouseClicked(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void mouseDragged(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void mouseMoved(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void mouseWheelMoved(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void inputEnded() {
    // TODO Auto-generated method stub

}

@Override
public void inputStarted() {
    // TODO Auto-generated method stub

}

@Override
public boolean isAcceptingInput() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void setInput(Input arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

@Override
public void keyReleased(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

@Override
public void controllerButtonPressed(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void controllerButtonReleased(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void controllerDownPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerDownReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerLeftPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerLeftReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerRightPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerRightReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerUpPressed(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void controllerUpReleased(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void enter(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}

@Override
public int getID() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void init(GameContainer gc, StateBasedGame sbg)
        throws SlickException {
    // TODO Auto-generated method stub

    PlayNow = new Image ("res/PlayNow.png");
    exitGame = new Image ("res/exitGame.png");

}

@Override
public void leave(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}

@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
        throws SlickException {
    // TODO Auto-generated method stub

    g.drawString("It's time for an adventure!", 50, 50);


    //g.drawRect(730, 320, 70, 80); //x, y, width, height

    PlayNow.draw(680, 320, 250, 50);
    exitGame.draw(680, 380, 250, 50);

    int xpos = Mouse.getX();

    int ypos = Mouse.getY();

    g.drawString("Mouse Position: " + xpos + " " + ypos, 10, 22);

}

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
        throws SlickException {
    // TODO Auto-generated method stub

    int xpos = Mouse.getX();

    int ypos = Mouse.getY();

    Input input = gc.getInput();

    //play now

    if( (xpos > 683) && (xpos < 920) && (ypos > 440) && (ypos < 478) ) {

        if(Mouse.isButtonDown(0)) {

            sbg.enterState(1);

        }

    //exit Game

    if ( (xpos > 683) && (xpos < 920) && (ypos > 379) && (ypos < 417) ) {

        if(Mouse.isButtonDown(0)) {

            gc.exit();

            }

        }

    }

    }


}

and there is one more class, but it doesn't matter. Please help!


回答1:


The problem is your second if statement is nested within the first. So, the first statement must be true for the second to exit. Unfortunately, for the first statement to be true, the second can never be true. So the second never executes.

The first if requires ypos>440 && ypos<478. The second if requires ypos>379 && <417. There is no overlap between those 2 ranges, and so the second if can never execute. Here's your code with cleaner formatting.

if( (xpos > 683) && (xpos < 920) && (ypos > 440) && (ypos < 478) ) {
    if(Mouse.isButtonDown(0)) {
        sbg.enterState(1);
    }
    //ypos will never be less than 417 because to reach this point it MUST
    //be greater than 440 due to first if statement.
    if ( (xpos > 683) && (xpos < 920) && (ypos > 379) && (ypos < 417) ) {
        if(Mouse.isButtonDown(0)) {
            gc.exit();
        }
    }
}

And here's the solution:

if( (xpos > 683) && (xpos < 920) && (ypos > 440) && (ypos < 478) ) {
    if(Mouse.isButtonDown(0)) {
        sbg.enterState(1);
    }
}
//exit Game
if ( (xpos > 683) && (xpos < 920) && (ypos > 379) && (ypos < 417) ) {
    if(Mouse.isButtonDown(0)) {
        gc.exit();
    }
}

Now the second if is independent of the first.



来源:https://stackoverflow.com/questions/18165812/slick-button-will-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!