Slick2d状态转换错误

时间:2014-05-19 08:53:49

标签: java state slick2d 2d-games

我按照教程制作了我的第一个slick2d游戏,不幸的是,当从播放基础状态转换到菜单状态时出现问题。 我尝试了很多东西,但我没有认识到这个问题

这是主要类(GAME)

package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Game extends StateBasedGame {
public static final  String gamename = "Ham Blaster";
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(menu));
this.addState(new Play(play));
this.addState(new Menu(play));

}
public void initsStatesList(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) {
    // TODO Auto-generated method stub
    AppGameContainer appgc;
    try {
        appgc = new AppGameContainer(new Game(gamename));
        appgc.setDisplayMode(640,360,false);
        appgc.start();

    }

    catch(SlickException e)
    {
e.printStackTrace();
}
}
public void initStatesList(GameContainer arg0) throws SlickException {
    // TODO Auto-generated method stub

}
}

菜单类

package javagame;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.*;


public class Menu extends BasicGameState implements GameState {
private int state;
Image playNow;
Image exitGame;

public Menu(int menu) {
    // TODO Auto-generated constructor stub

}

public void init(GameContainer arg0, StateBasedGame arg1)
        throws SlickException { 


}




public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
        throws SlickException {
    // TODO Auto-generated method stub

    playNow = new Image("res/playNow.png");
    exitGame=new Image("res/exitGame.png");

    g.drawString("Welcome to BuckyLand", 100, 50);
    g.drawImage(playNow, 100, 100);
    g.drawImage(exitGame, 100, 200);





}


public void update(GameContainer gc, StateBasedGame sbg, int arg2)
        throws SlickException {
int posX = Mouse.getX();
int posY = Mouse.getY();
if (posX > 100 && posX < 100+playNow.getWidth() && posY > 209 &&posY<260)
{
    if (Mouse.isButtonDown(0)) {
        sbg.enterState(1);
    }

}

if (posX > 100 && posX < 100+playNow.getWidth() && posY > 109 &&posY<160)
{
    if (Mouse.isButtonDown(0)) {
        System.exit(0);
    }

}
}



public void mouseClicked(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

public void mouseDragged(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

public void mouseMoved(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

public void mousePressed(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

public void mouseReleased(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

public void mouseWheelMoved(int arg0) {
    // TODO Auto-generated method stub

}

public void inputEnded() {
    // TODO Auto-generated method stub

}

public void inputStarted() {
    // TODO Auto-generated method stub

}

public boolean isAcceptingInput() {
    // TODO Auto-generated method stub
    return false;
}

public void setInput(Input arg0) {
    // TODO Auto-generated method stub

}

public void keyPressed(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

public void keyReleased(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

public void controllerButtonPressed(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

public void controllerButtonReleased(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

public void controllerDownPressed(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerDownReleased(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerLeftPressed(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerLeftReleased(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerRightPressed(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerRightReleased(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerUpPressed(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerUpReleased(int arg0) {
    // TODO Auto-generated method stub

}

public void enter(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}

public int getID() {
    // TODO Auto-generated method stub
    return 0;
}



public void leave(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}
}

Play Class

package javagame;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState {
Animation bucky,movingUp,movingDown,movingLeft,movingRight;
Image worldMap;
boolean quit = false;
int[] duration={200,200};
float buckyPosX = 0;
float buckyPosY = 0;
float shiftX = buckyPosX + 320;
float shiftY = buckyPosY + 160;


public Play(int play) {
    // TODO Auto-generated constructor stub

}

public void mouseClicked(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

public void mouseDragged(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

public void mouseMoved(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

public void mousePressed(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

public void mouseReleased(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

public void mouseWheelMoved(int arg0) {
    // TODO Auto-generated method stub

}

public void inputEnded() {
    // TODO Auto-generated method stub

}

public void inputStarted() {
    // TODO Auto-generated method stub

}

public boolean isAcceptingInput() {
    // TODO Auto-generated method stub
    return false;
}

public void setInput(Input arg0) {
    // TODO Auto-generated method stub

}

public void keyPressed(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

public void keyReleased(int arg0, char arg1) {
    // TODO Auto-generated method stub

}

public void controllerButtonPressed(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

public void controllerButtonReleased(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

public void controllerDownPressed(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerDownReleased(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerLeftPressed(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerLeftReleased(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerRightPressed(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerRightReleased(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerUpPressed(int arg0) {
    // TODO Auto-generated method stub

}

public void controllerUpReleased(int arg0) {
    // TODO Auto-generated method stub

}

public void enter(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}

public int getID() {
    // TODO Auto-generated method stub

    return 1;
}

public void init(GameContainer arg0, StateBasedGame sbg)
        throws SlickException {
    // TODO Auto-generated method stub
    worldMap = new Image("res/world.png");
    Image[] walkUp = {new Image("/res/buckysBack.png"),new                                                                         
             Image("res/buckysBack.png")};
    Image[] walkDown = {new Image("/res/buckysFront.png"),new   
            Image("res/buckysFront.png")};
    Image[] walkLeft = {new Image("/res/buckysLeft.png"),new 
            Image("res/buckysLeft.png")}; 
    Image[] walkRight = {new Image("/res/buckysRight.png"),new          
            Image("res/buckysRight.png")};
    movingUp = new Animation(walkUp,duration,false);
    movingDown = new Animation(walkDown,duration,false);
    movingLeft = new Animation(walkLeft,duration,false);
    movingRight = new Animation(walkRight,duration,false);
    bucky = movingDown;




}

public void leave(GameContainer arg0, StateBasedGame arg1)
        throws SlickException {
    // TODO Auto-generated method stub

}

public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
        throws SlickException {
    // TODO Auto-generated method stub
    worldMap.draw(buckyPosX,buckyPosY);
    bucky.draw(shiftX, shiftY);
    g.drawString("Bucky Xpos = "+buckyPosX+"\nBucky YPos = "+buckyPosY,400,20);
    if(quit)
    {
        g.drawString("Resume(R)\nMenu(M)\nExit(Q)", 150, 200);
        if(!quit) g.clear();
    }
}

public void update(GameContainer gc, StateBasedGame sbg, int delta)
        throws SlickException {
    // TODO Auto-generated method stub
    Input input = gc.getInput();
    if(!quit)
    {
    if(input.isKeyDown(Input.KEY_UP))
    {   bucky = movingUp;
    buckyPosY += delta * 0.1f;
    if(buckyPosY > 162)
        buckyPosY -= delta * 0.1f;

    }
    if(input.isKeyDown(Input.KEY_DOWN))
    {   bucky = movingDown;
    buckyPosY -= delta * 0.1f;
    if(buckyPosY <- 600)
        buckyPosY += delta * 0.1f;  

    }   
    if(input.isKeyDown(Input.KEY_LEFT))
    {   bucky = movingLeft;
    buckyPosX += delta * 0.1f;
    if(buckyPosX > 324)
        buckyPosX -= delta * 0.1f;
    }
    if(input.isKeyDown(Input.KEY_RIGHT))
    {   bucky = movingRight;
    buckyPosX -= delta * 0.1f;
    if(buckyPosX < -840)
        buckyPosX += delta * 0.1f;
    }}

    if(input.isKeyDown(Input.KEY_ESCAPE))
        quit = true;
    if(quit)
    {
        if(input.isKeyDown(Input.KEY_R))
            quit = false;
        if(input.isKeyDown(Input.KEY_M))
         sbg.enterState(1);
        if(input.isKeyDown(Input.KEY_M))
             System.exit(0);

    }


}

}

非常感谢!!

1 个答案:

答案 0 :(得分:1)

您正在播放状态中切换到播放状态。在Play类中,添加sbg.enterState(0)而不是sbg.enterState(1)