错误:Game.update()失败Slick 2D碰撞检测

时间:2013-12-14 21:44:55

标签: java collision-detection slick2d

我正在使用slick2D创​​建一个测试游戏,现在我正在使用矩形和交叉方法进行命中检测。 我为玩家接触的每个平台创建了两个矩形;一个用于查看玩家是从侧面还是底部击中平台,另一个像素高的矩形用于查看角色是否站在平台上。

问题是,如果我将更多类似于平台(一个像素长矩形)的内容添加到更新类,例如:

if(playplathit.intersects(plat1top))
{
    falling = false;
}

当您进入平台所处的状态时,您会收到此错误:

Fri Dec 13 22:57:31 EST 2013 ERROR:Game.update() failure - check the game code.
org.newdawn.slick.SlickException: Game.update() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(Ga meContainer.java:669)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGam eContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameCo ntainer.java:321)
at javagame.Game.main(Game.java:34)

我的猜测是我有点过载Java或Slick。我也确信我正在以最糟糕的方式进行命中检测,因为我没有遵循指南。 以下是更新类中没有任何plattop if语句的代码:

public class Play extends BasicGameState
{
    int[] duration = {200,200};
    float playerX = 100;
    float playerY = 353;
    float screenX = 0;
    float screenY = 0;
    float plat1X = 0;
    float plat2X = 174;
    float plat3X = 461;
    float plat4X = 618;
    float plat5X = 881;
    float plat6X = 1233;
    float plat7X = 1438;
    float plat8X = 1754;
    float vertspd = 0.0f;
    Animation playeranistart, idleani, runforani, runbackani, jumpani;
    boolean falling;
    boolean jumping;
    boolean rightside = false;
    boolean leftside = false;
    Rectangle playplathit, plat1, plat2, plat3, plat4, plat5, plat6, plat7, plat8, plat1top, plat2top, plat3top, plat4top, plat5top, plat6top, plat7top, plat8top;
    public Play(int state)
    {

    }
    public void player()
    {

    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
        Image[] idle = {new Image("res/player.png"), new Image("res/player.png")};
        idleani = new Animation(idle,duration,false);
        Image[] startidle = {new Image("res/player.png"), new Image("res/player.png")};
        playeranistart = new Animation(startidle,duration,false);
        Image[] run1 = {new Image("res/playerrun1.png"), new Image("res/playerrun1.png")};
        runforani = new Animation(run1,duration,false);
        Image[] run2 = {new Image("res/playerrun2.png"), new Image("res/playerrun2.png")};
        runbackani = new Animation(run2,duration,false);

    }
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
    {
        Image lvl1 = new Image("res/testlvl.png");
        playplathit = new Rectangle(playerX, playerY, 15, 39);
        plat1 = new Rectangle(plat1X, 393, 597, 8);
        plat1top = new Rectangle(plat1X, 392, 597, 1);
        plat2 = new Rectangle(plat2X, 325, 247, 5);
        plat2top = new Rectangle(plat2X, 324, 247, 1);
        plat3 = new Rectangle(plat3X, 265, 78, 6);
        plat3top = new Rectangle(plat3X, 264, 78, 1);
        plat4 = new Rectangle(plat4X, 232, 141, 5);
        plat4top = new Rectangle(plat4X, 231, 141, 1);
        plat5 = new Rectangle(plat5X, 393, 755, 8);
        plat5top = new Rectangle(plat5X, 392, 755, 1);
        plat6 = new Rectangle(plat6X, 357, 214, 44);
        plat6top = new Rectangle(plat6X, 356, 214, 1);
        plat7 = new Rectangle(plat7X, 330, 219, 70);
        plat7top = new Rectangle(plat7X, 329, 219, 1);
        plat8 = new Rectangle(plat8X, 393, 243, 8);
        plat8top = new Rectangle(plat8X, 392, 243, 1);
        g.drawImage(lvl1, screenX,screenY);
        playeranistart.draw(playerX,playerY);
        g.setColor(Color.red);
        g.draw(playplathit);
        g.draw(plat1);
        g.draw(plat2);
        g.draw(plat3);
        g.draw(plat4);
        g.draw(plat5);
        g.draw(plat6);
        g.draw(plat7);
        g.draw(plat8);
        g.draw(plat1top);
        g.draw(plat2top);
        g.draw(plat3top);
        g.draw(plat4top);
        g.draw(plat5top);
        g.draw(plat6top);
        g.draw(plat7top);
        g.draw(plat8top);
    }
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
         Input input = gc.getInput();

         if(jumping == false)
         {
             vertspd = 0;
         }
         if((input.isKeyDown(Input.KEY_D) == false) || (input.isKeyDown(Input.KEY_A) == false)|| (input.isKeyDown(Input.KEY_W) == false))
         {
             playeranistart = idleani;
         }
         if(input.isKeyDown(Input.KEY_D))
         {
             screenX = screenX - 3.3f;
             plat1X = plat1X - 3.3f;
             plat2X = plat2X - 3.3f;
             plat3X = plat3X - 3.3f;
             plat4X = plat4X - 3.3f;
             plat5X = plat5X - 3.3f;
             plat6X = plat6X - 3.3f;
             plat7X = plat7X - 3.3f;
             plat8X = plat8X - 3.3f;
             playeranistart = runforani;
             if(playplathit.intersects(plat1) || playplathit.intersects(plat2) || playplathit.intersects(plat3) || playplathit.intersects(plat4) || playplathit.intersects(plat5) || playplathit.intersects(plat6) || playplathit.intersects(plat7) || playplathit.intersects(plat8))
             {
                 if(rightside)
                 {
                     plat1X = plat1X - 3.3f;
                     plat2X = plat2X - 3.3f;
                     plat3X = plat3X - 3.3f;
                     plat4X = plat4X - 3.3f;
                     plat5X = plat5X - 3.3f;
                     plat6X = plat6X - 3.3f;
                     plat7X = plat7X - 3.3f;
                     plat8X = plat8X - 3.3f;
                     screenX = screenX - 3.3f;
                     rightside = false;
                 }
                 else
                 {
                     plat1X = plat1X + 3.3f;
                     plat2X = plat2X + 3.3f;
                     plat3X = plat3X + 3.3f;
                     plat4X = plat4X + 3.3f;
                     plat5X = plat5X + 3.3f;
                     plat6X = plat6X + 3.3f;
                     plat7X = plat7X + 3.3f;
                     plat8X = plat8X + 3.3f;
                     screenX = screenX + 3.3f; 
                     leftside = true;
                 }
             }
         }
         if(input.isKeyDown(Input.KEY_A))
         {
             playeranistart = runbackani;
             plat1X = plat1X + 3.3f;
             plat2X = plat2X + 3.3f;
             plat3X = plat3X + 3.3f;
             plat4X = plat4X + 3.3f;
             plat5X = plat5X + 3.3f;
             plat6X = plat6X + 3.3f;
             plat7X = plat7X + 3.3f;
             plat8X = plat8X + 3.3f;
             screenX = screenX + 3.3f;
             if(playplathit.intersects(plat1) || playplathit.intersects(plat2) || playplathit.intersects(plat3) || playplathit.intersects(plat4) || playplathit.intersects(plat5) || playplathit.intersects(plat6) || playplathit.intersects(plat7) || playplathit.intersects(plat8))
             {

                 if(leftside)
                 {
                     plat1X = plat1X + 3.3f;
                     plat2X = plat2X + 3.3f;
                     plat3X = plat3X + 3.3f;
                     plat4X = plat4X + 3.3f;
                     plat5X = plat5X + 3.3f;
                     plat6X = plat6X + 3.3f;
                     plat7X = plat7X + 3.3f;
                     plat8X = plat8X + 3.3f;
                     screenX = screenX + 3.3f;
                     leftside = false;
                 }
                 else
                 {
                     plat1X = plat1X - 3.3f;
                     plat2X = plat2X - 3.3f;
                     plat3X = plat3X - 3.3f;
                     plat4X = plat4X - 3.3f;
                     plat5X = plat5X - 3.3f;
                     plat6X = plat6X - 3.3f;
                     plat7X = plat7X - 3.3f;
                     plat8X = plat8X - 3.3f;
                     screenX = screenX - 3.3f;
                     rightside = true;
                 }
             }
         }
         if(input.isKeyDown(Input.KEY_W))
         {
             if(jumping != true)
             {
                 vertspd = 1.0f * (delta);
                 playerY = playerY - vertspd;

             }   
         }
    }
    public int getID()
    {
        return 1;
    }
}

感谢您的帮助。

0 个答案:

没有答案