2013-08-10 7 views
0

자바 게임에 대한 비디오 튜토리얼을 따르고 있습니다.하지만 거기에서 벗어날 수는 있지만 버튼 중 하나는 작동하지 않습니다 (종료 버튼). 도와주세요. 나는 lwjgl을 사용하고 있습니다.슬릭 버튼이 작동하지 않습니다.

메인 클래스 :

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

      } 

     } 

    } 

    } 


} 

거기에 하나 개 더 클래스이지만, 그것은 중요하지 않습니다. 도와주세요!

+0

이탈 버튼에 대한 코드가 표시되지 않습니다. –

+0

미안하지만 같은 것을 2 개 게시했습니다 –

+0

거기서 고쳐주었습니다. –

답변

0

문제는 두 번째 if 문이 첫 번째 내에서 중첩 된 것입니다. 따라서 첫 번째 문은 두 번째 문이 종료 될 때까지 true 여야합니다. 불행히도 첫 번째 진술이 사실이라면 두 번째 진술은 사실 일 수 없습니다. 그래서 두 번째는 결코 실행되지 않습니다.

첫 번째 ifypos>440 && ypos<478이 필요합니다. 두 번째 ifypos>379 && <417이 필요합니다. 이 두 범위 사이에는 겹침이 없으므로 두 번째 if은 절대로 실행될 수 없습니다. 보다 명확한 형식의 코드가 있습니다.

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이 첫 번째의 독립적 인 :

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

그리고 여기에 솔루션입니다.

관련 문제