输入状态错误,Slick2d

时间:2014-02-20 02:33:29

标签: java graphics state slick2d flappy-bird-clone

我正在创造一个飞扬的鸟类esque迷你游戏。由于某种原因,当我尝试从“菜单”状态进入我的实际“播放”状态并且“级别”状态将它们全部保持在一起时出现错误,因为它之前正在工作时抛出新的错误。非常感谢任何帮助,谢谢!

等级状态:将它们组合在一起:

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

1 个答案:

答案 0 :(得分:0)

我建议更改“级别”状态,以便正确使用光头StateBasedGame。

在光滑的wiki上检查wiki条目: http://slick.ninjacave.com/wiki/index.php?title=Game_States

请注意给定示例(特别是类构造函数方法和initStateList()方法)与您的关卡类中的内容之间的区别。注意如何调用state.init(gc,this),因为添加一个状态会调用它自己的init方法。这可能是你问题的答案,因为我之前已经看到过类似的情况。