이 코드를 사용하면 함께 던져지는 귀여운 게임을 만들 수 있습니다. 이론적으로 level.java는 Menu.java를 시작해야하고 화면에서 재생 버튼을 클릭하면 Playing.java 상태로 전환해야합니다. 불행히도 이런 일이 생기면 (재생 버튼을 클릭하는 것,이 경우 동전) 나는 적어도 내 콘솔에서 횡설수설을 겪는다. 그래서 내가 너희에게 묻는 것은 이것이다. 왜 이런 일이 일어날 수 있습니까? 가능한 해결책은 무엇입니까? 그리고 내 콘솔에 나오는 내용을 해독하기위한 디버깅 요령이 있습니까? 을 heres "수준"여기상태를 움직이는 데 문제가있는 slick2D 사용
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class level extends StateBasedGame{
public static final String gamename = "Flight 1.0 ";
public static final int menu = 0;
public static final int playing = 1;
public level(String gamename){
super(gamename);
this.addState(new Menu(menu));
this.addState(new Playing(playing));
}
public void initStatesList(GameContainer gc)throws SlickException{
this.getState(menu).init(gc, this);
this.getState(playing).init(gc, this);
this.enterState(menu);
}
public static void main(String args[]){
AppGameContainer appgc;
try{
appgc = new AppGameContainer(new level(gamename));
appgc.setDisplayMode(800, 600, false);
appgc.start();
}catch(SlickException e){
e.printStackTrace();
}}}
그리고는 재생 버튼 "메뉴"입니다 : 마지막으로
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState{
Image play;
int xpos = Mouse.getX();
int ypos = Mouse.getY();
public Menu(int state){
}
public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
play = new Image("Resources\\coin.gif");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
g.drawImage(play, 0, 0);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
Input input = gc.getInput();
if((xpos >= 0 && xpos <= 127) && (ypos>= 0 && ypos <= 33)){
if(input.isMouseButtonDown(0)){
sbg.enterState(1);
}
}
}
public int getID(){
return 0;
}
}
그리고 I가 전송하려고 시도하고있어 것을 "재생"
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Playing extends BasicGameState{
//game objects
Image guy;
Image coin1;
Image coin2;
//object coordinates
float x = 30, y = 50;
float coin1x = 1000, coin1y = 500;
float coin2x = 1100, coin2y = 400;
float rect1x = 1000, rect1y = 225;
//coin counter
int coincount = 0;
//ignore
public Playing(int state){}
//declare items
public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
guy = new Image("Resources\\PlaneMovie1.gif");
coin1 = new Image("Resources\\coin.gif");
}
//draw items
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
g.drawImage(guy, x, y);
g.drawImage(coin1, coin1x, coin1y);
g.drawImage(coin2, coin2x, coin2y);
g.drawString("Coins: " + coincount, 100, 100);
g.drawRect(rect1x, rect1y, 50, 425);
}
//declare changes to items
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
//plane movement/controls
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_SPACE)){ y -= 1 ;}
if(input.isKeyDown(Input.KEY_SPACE) == false){
y += .5;
}
//coin counter
if (x == coin2x && y == coin2y){
coincount +=1 ;
}
if(x == coin1x && y == coin1y){
coincount += 1;
}
//coin 1
coin1x --;
if(coin1x <= -100){
coin1x = 1000; coin1y -= 100;
}
if (coin1y <= 100){
coin1y = 500;
}
//coin 2
coin2x --;
if(coin2x <= -100){
coin2x = 1100; coin2y -= 100;
}
if (coin2y <= 100){
coin2y = 500;
}
//rect1
rect1x --;
if(rect1x <= -100){
rect1x = 1000; rect1y -= 100;
}
if (coin1y <= 100){
rect1y = 425;
}
}
//ignore
public int getID(){
return 1;
}
}
어떤 도움을 주셔서 감사합니다. StackOverflow에 대한 첫 번째 게시물이므로 건설적인 비판을 크게 환영합니다.
을 특별히하는 상황에서 "재생"버튼의 클릭을 전송 : 여기에
마우스 좌표를 갱신 (및 이미지 좌표로 Y가 같은 형식으로 좌표를 설정 편집 업데이트 방법 상태가 그렇다면 솔루션을 상상해보십시오. – user2867659업데이트 된 좌표를 가져 오는 데 대한 자세한 내용을 포함하도록 답변을 업데이트했습니다 .Y 좌표는 여전히 반전됩니다. – Hayden
스크래치는 업데이트 된 부분에 넣을 때 (오른쪽으로 뒤집 혔을 때) slick2D와 어떻게 일치해야할까요? 버튼이 있어야하는 새로운 영역을 클릭하면 똑같은 충돌이 발생합니다. – user2867659