油滑按钮将无法正常工作 [英] Slick button will not work

查看:119
本文介绍了油滑按钮将无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的Java游戏的视频教程(这样我就可以从那里过),但我的一个按钮将无法正常工作(退出按钮)。请帮忙。我使用LWJGL和光滑。

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.

主类:

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

            }

        }

    }

    }


}

和还有一个类,但没关系。请帮助!

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

推荐答案

问题是你的第二个如果语句嵌套中的第一个。所以,第一个语句必须是真正的第二个退出。不幸的是,对于第一个语句为真时,第二可永远不会为真。所以第二个从未执行。

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.

第一个如果要求 ypos&GT; 440安培;&安培; ypos&LT; 478 。第二个如果要求 ypos&GT; 379安培;&安培; &LT; 417 。有所以第二个如果不能执行这些2范围之间没有重叠。这是你的code与清洁格式。

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

和这里的解决方案:

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

现在第二如果独立之首。

这篇关于油滑按钮将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆