Question

Okay with this code, I have a cute little game thrown together. In theory level.java should initiate Menu.java, and once the play button is clicked on screen you should transfer to the Playing.java state. Unfortunately when this happens (the clicking of the play button, in this case a coin) I get loads of gibberish in my console (to me at least). So what i'm asking of you guys is this. Why could this be happening? What are some possible solutions? and are there any debugging tips for deciphering what comes up in my console? Heres the "level":

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

And here is the "Menu" with the play button:

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

And finally the "Playing" that I'm attempting to transfer to:

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

Any help is greatly appreciated, this is my first post to StackOverflow, so constructive criticism is greatly appreciated.

Was it helpful?

Solution

Mouse coordinates in Slick2d are reversed compared to graphics coordinates. For example:

Mouse Y will be equal to your screen height when you are at (0,0). I hope this solves your problem.

Edit

Upon further look, I found that the coin2 in Playing class doesn't have an image assigned to it so it was crashing because of nullpointer exception i.e. you need to assign an image like the other ones.

Here is the edited update method which updates the mouse coordinates (and sets the Y coordinates to the same format as Image coordinates:

public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
    xpos = Mouse.getX();
    // 600 is the Screen height
    ypos = Math.abs(600-Mouse.getY());

  if((xpos >= 0 && xpos <= 127) && (ypos>= 0 && ypos <= 33)){
   if(Mouse.isButtonDown(0)){
    sbg.enterState(1);
   }
  }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top