0

我正在关注一个关于 java 游戏的视频教程(所以我可以从那里开始),但我的一个按钮不起作用(退出按钮)。请帮忙。我正在使用 lwjgl 和 slick。

主类:

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

    }



}

}

菜单类:

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

            }

        }

    }

    }


}

还有一门课,不过没关系。请帮忙!

4

1 回答 1

0

问题是您的第二个if语句嵌套在第一个语句中。因此,第一条语句必须为真,第二条语句才能退出。不幸的是,要使第一个陈述为真,第二个永远不可能为真。所以第二个永远不会执行。

第一个if需要ypos>440 && ypos<478. 第二个if要求ypos>379 && <417。这两个范围之间没有重叠,因此第二个范围if永远无法执行。这是您的代码格式更清晰。

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

这是解决方案:

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

现在第二个if独立于第一个。

于 2013-08-10T20:14:48.067 回答