我正在创建一个飞扬的鸟式小游戏。由于某种原因,当我尝试从"菜单"状态进入实际的"播放"状态并且"级别"状态将其全部固定在一起时会出现错误,并且会抛出一个新错误,因为它以前工作。任何帮助将不胜感激,谢谢!
关卡状态:将所有内容固定在一起:
package gamepackage;
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, 550, false);
appgc.start();
}catch(SlickException e){
e.printStackTrace();
}
}
}
我的菜单状态:是玩家单击播放进入"正在播放"状态的地方:
package gamepackage;
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("res/play.jpg");
}
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;
}
}
游戏状态,游戏发生的位置:
package gamepackage;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.*;
public class Playing extends BasicGameState{
Image guy;
Image bg1;
Image bg2;
Image bg3;
Rectangle pr;
Rectangle rect1, rect2, rect3, rect4, rect5;
Rectangle rect1d, rect2d, rect3d, rect4d, rect5d;
float bg1x = 0;
float bg2x = 800;
float bg3x = 1600;
float x = 30, y = 90;
float px = 30, py = y + 160;
float rect1x = 1000, rect1y = 0;
float rect2x = 1250, rect2y = 0;
float rect3x = 1500, rect3y = 0;
float rect4x = 1750, rect4y = 0;
float rect5x = 2000, rect5y = 0;
float rect1dx = 1000, rect1dy = 300;
float rect2dx = 1250, rect2dy = 350;
float rect3dx = 1500, rect3dy = 400;
float rect4dx = 1750, rect4dy = 350;
float rect5dx = 2000, rect5dy = 300;
public Playing(int state){
}
public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
bg1 = new Image("res/bg1.jpg");
bg2 = new Image("res/bg2.jpg");
bg3 = new Image("res/bg3.jpg");
guy = new Image("res/PlaneMovie1.gif");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
g.drawImage(bg1, bg1x, 0);
g.drawImage(bg2, bg2x, 0);
g.drawImage(bg1, bg3x, 0);
pr = new Rectangle(240, py, 135, 80);
rect1 = new Rectangle(rect1x, rect1y, 50, 100);
rect1d = new Rectangle(rect1dx, rect1dy, 50, 300);
rect2 = new Rectangle(rect2x, rect2y, 50, 150);
rect2d = new Rectangle(rect2dx, rect2dy, 50, 300);
rect3 = new Rectangle(rect3x, rect3y, 50, 200);
rect3d = new Rectangle(rect3dx, rect3dy, 50, 300);
rect4 = new Rectangle(rect4x, rect4y, 50, 150);
rect4d = new Rectangle(rect4dx, rect4dy, 50, 300);
rect5 = new Rectangle(rect5x, rect5y, 50, 100);
rect5d = new Rectangle(rect5dx, rect5dy, 50, 300);
g.draw(rect1);
g.draw(rect1d);
g.draw(rect2);
g.draw(rect2d);
g.draw(rect3);
g.draw(rect3d);
g.draw(rect4);
g.draw(rect4d);
g.draw(rect5);
g.draw(rect5d);
g.drawImage(guy, x, y);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_SPACE)){ y -= .3 ; py -= .3 ; }
if(input.isKeyDown(Input.KEY_SPACE) == false){ y += .4; py += .4;}
if(rect1x == -100){rect1x += 1150; rect1dx += 1150;}
if(rect2x == -100){rect2x += 1150; rect2dx += 1150;}
if(rect3x == -100){rect3x += 1150; rect3dx += 1150;}
if(rect4x == -100){rect4x += 1150; rect4dx += 1150;}
if(rect5x == -100){rect5x += 1150; rect5dx += 1150;}
//collision detection
if(pr.intersects(rect1)){ System.out.println("eyyo.");}
bg1x -= .20;
bg2x -= .20;
bg3x -= .20;
rect1x-= .25;
rect2x -= .25;
rect3x -= .25;
rect4x -= .25;
rect5x -= .25;
rect1dx -= .25;
rect2dx -= .25;
rect3dx -= .25;
rect4dx -= .25;
rect5dx -= .25;
}
public int getID(){
return 1;
}
}
我建议更改您的"级别"状态,以遵循正确使用光滑的StateBasedGame。
在光滑的维基上检查维基条目:http://slick.ninjacave.com/wiki/index.php?title=Game_States
请注意给出的示例(特别是类构造函数方法和 initStateList() 方法)与 level 类中的区别。 请注意,没有必要调用state.init(gc,this),因为添加状态会自行调用init方法。这可能是您问题的答案,因为我以前见过类似的情况。