Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Need help swapping between States (FIXED)  (Read 424 times)
0 Members and 1 Guest are viewing this topic.
Offline mana

Senior Newbie





« Posted 2012-06-12 03:35:07 »

Hey, I'm having difficulty figuring out why I cannot swap between 2 different states, the game just crashes after I click the button to make the switch. Was just wondering if someone could give me a hand?

I run the game, click on the high scores button, then the display closes. I was following the SlickBox example http://slick.cokeandcode.com/wiki/doku.php?id=02_-_slickblocks

Sorry about the wall of code, but I really don't know were the issue is.

EDIT: after a bit more debugging it seems to be on the input, when I click anywhere on the screen, it just closes the display, is there anything that could be causing that?

Heres my Main file:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
 
/**
 * @author Tyler Matchett
 */

public class CodaClient extends StateBasedGame {
 
   public static final int MAINMENUSTATE = 0;
    public static final int GAMEPLAYSTATE = 1;
    public static final int HIGHSCORESTATE = 2;
    //public static final int OPTIONSSTATE = 3;
   //public static final int LOGINSTATE = 4;

    public CodaClient()
    {
        super("Coda - The Code Breaking Game");
 
        this.addState(new MainMenuState(MAINMENUSTATE));
        this.addState(new GameplayState(GAMEPLAYSTATE));
        this.addState(new HighscoreState(HIGHSCORESTATE));
        //this.addState(new OptionsState(OPTIONSSTATE));
       //this.addState(new LoginState(LOGINSTATE));
       this.enterState(MAINMENUSTATE);
    }
 
    public static void main(String[] args) throws SlickException
    {
         AppGameContainer app = new AppGameContainer(new CodaClient());
 
         app.setDisplayMode(800, 600, false);
         app.start();
    }
 
    @Override
    public void initStatesList(GameContainer gameContainer) throws SlickException {
 
        this.getState(MAINMENUSTATE).init(gameContainer, this);
        this.getState(GAMEPLAYSTATE).init(gameContainer, this);
        this.getState(HIGHSCORESTATE).init(gameContainer, this);
        //this.getState(OPTIONSSTATE).init(gameContainer, this);
       //this.getState(LOGINSTATE).init(gameContainer, this);
   }
}


The state I start in:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
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.Sound;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
 
public class MainMenuState extends BasicGameState {
 
    int stateID = 0;
   
    // Image declarations
  Image bkg = null;
   Image loginbkg = null;
   Image logo = null;
   Image play_button = null;
   Image highscore_button = null;
   Image options_button = null;
   Image quit_button = null;
   Image login_button = null;
   
   // Button effects
  float playScale = 1;
   float highscoreScale = 1;
   float optionsScale = 1;
    float exitScale = 1;
    float loginScale = 1;
   
   float scaleStep = 0.0001f;
   
   // Menu Sound Effects
  //Sound fx = null;

    MainMenuState(int stateID)
    {
       this.stateID = stateID;
    }
 
    @Override
    public int getID() {
        return stateID;
    }
 
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
       // Load the images
      bkg = new Image("res/bkg.jpg");
      loginbkg = new Image("res/loginbkg.jpg");
      logo = new Image("res/logo.png");
      play_button = new Image("res/btnPlay.png");
      highscore_button = new Image("res/btnHighscore.png");
      options_button = new Image("res/btnOptions.png");
      quit_button = new Image("res/btnQuit.png");
      login_button = new Image("res/btnLogin.png");
     
      // Load Sounds
     //fx = new Sound("res/sounds/menu_selection.wav");
   }
 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
       // draw the intro menu
     bkg.draw(0, 0);
      logo.draw(470, 90);
      play_button.draw(0, 90, playScale);
      highscore_button.draw(0, 196, highscoreScale);
      options_button.draw(0, 277, optionsScale);
      quit_button.draw(0, 358, exitScale);
      login_button.draw(0, 505, loginScale);
     
      g.drawString("Username [Highscore]", 570f, 510f);
      g.drawString("Main Menu", 50f, 50f);
    }
 
    public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {
       Input input = gc.getInput();
       
       int mouseX = input.getMouseX();
       int mouseY = input.getMouseY();
       
       boolean insidePlay = false;
       boolean insideHighscore = false;
       boolean insideOptions = false;
       boolean insideQuit = false;
       boolean insideLogin = false;
       
       // Play button
      if( ( mouseX >= 0 && mouseX <= play_button.getWidth()) && ( mouseY >= 90 && mouseY <= 90 + play_button.getHeight()) ){
          insidePlay = true;
       } // Highscore button
      else if( ( mouseX >= 0 && mouseX <= highscore_button.getWidth()) && ( mouseY >= 196 && mouseY <= 196 + highscore_button.getHeight()) ){
            insideHighscore = true;
        } // Options button
      else if( ( mouseX >= 0 && mouseX <= options_button.getWidth()) && ( mouseY >= 277 && mouseY <= 277 + options_button.getHeight()) ){
          insideOptions = true;
        } // Quit button
      else if( ( mouseX >= 0 && mouseX <= quit_button.getWidth()) && ( mouseY >= 358 && mouseY <= 358 + quit_button.getHeight()) ){
          insideQuit = true;
       } // Login button
      else if( ( mouseX >= 0 && mouseX <= login_button.getWidth()) && ( mouseY >= 505 && mouseY <= 505 + login_button.getHeight()) ){
          insideLogin = true;
       }
       
       if(insidePlay){
              if(playScale < 1.015f)
                 playScale += scaleStep * delta;
         
              if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
                 sb.enterState(CodaClient.GAMEPLAYSTATE);
              }
         }else{
            if(playScale > 1.0f)
               playScale -= scaleStep * delta;
         
              if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
                 gc.exit();
              }
         }
       
       if(insideHighscore){
              if(highscoreScale < 1.015f)
                 highscoreScale += scaleStep * delta;
         
              if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
                 sb.enterState(CodaClient.HIGHSCORESTATE);
              }
         }else{
            if(highscoreScale > 1.0f)
               highscoreScale -= scaleStep * delta;
         
              if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
               gc.exit();
            }
         }
       
       if(insideOptions){
              if(optionsScale < 1.015f)
                 optionsScale += scaleStep * delta;
         
              if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
                 //sb.enterState(CodaClient.OPTIONSSTATE);
             }
         }else{
            if(optionsScale > 1.0f)
               optionsScale -= scaleStep * delta;
         
              if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
               gc.exit();
            }
         }
       
       if(insideQuit)
       {
          if(exitScale < 1.015f)
             exitScale +=  scaleStep * delta;
       }else{
          if(exitScale > 1.0f)
             exitScale -= scaleStep * delta;
       }
       
       if(insideLogin){
              if(loginScale < 1.015f)
                 loginScale += scaleStep * delta;
         
              if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
                 //sb.enterState(CodaClient.LOGINSTATE);
             }
         }else{
            if(loginScale > 1.0f)
               loginScale -= scaleStep * delta;
         
            if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
               gc.exit();
              }
         }
    }
}


The state I want to switch to:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
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.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
 
public class HighscoreState extends BasicGameState {
 
    int stateID = 2;
 
    // Image declarations
  Image bkg = null;
   
    HighscoreState(int stateID)
    {
       this.stateID = stateID;
    }
 
    @Override
    public int getID() {
        return stateID;
    }
 
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
       // Load the images
      bkg = new Image("res/bkg.jpg");
    }
 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
       // draw the intro menu
     bkg.draw(0, 0);
      g.drawString("Highscores", 50f, 50f);
    }
 
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
       Input input = gc.getInput();
       
       int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();
    }
 
}
Offline mana

Senior Newbie





« Reply #1 - Posted 2012-06-12 03:51:08 »

FIXED: It didn't have to do with the states at all, it was the in the mouse check, was exiting the display if it was outside every button (which it would be no matter what).
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (22 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (67 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (55 views)
2014-09-08 06:06:29

BurntPizza (39 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (30 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!