Show Posts
|
|
Pages: [1]
|
|
8
|
Game Development / Newbie & Debugging Questions / Re: [States] [Networking] [slick2d] Game state is being initialized when not wanted
|
on: 2012-06-13 09:06:35
|
|
I'll post the full source, but the issue is that it's even running that code, I don't need that code until it has the data that I declare earlier in the program. Basically in the menus I get the user to login, then it sends those credentials to the server in the GameState. But on Launch the GameState init() is being run when I dont want to call it until I need it.
Ill post full source code once I copy it to the bin, but would there be a better way to approach something like this?
|
|
|
|
|
10
|
Game Development / Newbie & Debugging Questions / [States] [Networking] [slick2d] Game state is being initialized when not wanted
|
on: 2012-06-13 08:48:50
|
Seems like my game state is being initialized even though I have it commented out in the initStatesList(...) is there a reason for that? I have it initializing network code would that affect the initializing? the exact error im getting is: Unable to get I/O connection to: localhost on port: 1967 Exception in thread "main" java.lang.NullPointerException at GameplayState.init(GameplayState.java:136) at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:171) at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390) at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314) at CodaClient.main(CodaClient.java:33) but the only connection attempt to localhost is in the game state Heres my initStatesList: 1 2 3 4 5 6
| public void initStatesList(GameContainer gameContainer) throws SlickException { this.getState(MAINMENUSTATE).init(gameContainer, this); this.getState(VICTORYSTATE).init(gameContainer, this); this.getState(OPTIONSSTATE).init(gameContainer, this); } |
and the states added to the state list: 1 2 3 4 5 6 7 8
| super("Title");
this.addState(new MainMenuState(MAINMENUSTATE)); this.addState(new GameplayState(GAMEPLAYSTATE)); this.addState(new VictoryState(VICTORYSTATE)); this.addState(new OptionsState(OPTIONSSTATE));
this.enterState(MAINMENUSTATE); |
Complete Source Code: CodaClient.java: http://pastebin.com/SvGnbVXrMainMenuState.java: http://pastebin.com/W8bBDbn6GameplayState.java: http://pastebin.com/rXm0kkikOptionsState.java: http://pastebin.com/wStTBq0LVictoryState.java: http://pastebin.com/gvUpGJf9Based on slickBox Tutorial: http://slick.cokeandcode.com/wiki/doku.php?id=02_-_slickblocksEdit: couldn't get the JGO files to work so I just used pastebin
|
|
|
|
|
12
|
Game Development / Newbie & Debugging Questions / [Slick2d] [input] Keys and mouse firing too fast
|
on: 2012-06-13 03:35:10
|
Hey I'm having an issue were, when clicking on a button or when clicking a key, the key or mouse click fires like 10 responses (restricting my game to 30fps reduces the number to that, without the fps reduction its closer to 100) I'm just wondering how I can maintain that when I click or hit a button, it with only receive one response? heres some of my input code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| if( ( mouseX >= 0 && mouseX <= play_button.getWidth()) && ( mouseY >= 90 && mouseY <= 90 + play_button.getHeight()) ){ insidePlay = true; }
if(insidePlay){ if(playScale < 1.015f) playScale += scaleStep * delta; if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){ if(!isLogin) { tryLogin = true; } else { sb.enterState(CodaClient.GAMEPLAYSTATE); } } }else{ if(playScale > 1.0f) playScale -= scaleStep * delta; } |
EDIT: Sorry if there's another thread with a similar question, I searched threw a few pages but wasn't sure what this exact issue is called, so I didnt find much that helped.
|
|
|
|
|
14
|
Game Development / Newbie & Debugging Questions / Need help swapping between States (FIXED)
|
on: 2012-06-12 05: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_-_slickblocksSorry 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;
public class CodaClient extends StateBasedGame { public static final int MAINMENUSTATE = 0; public static final int GAMEPLAYSTATE = 1; public static final int HIGHSCORESTATE = 2; public CodaClient() { super("Coda - The Code Breaking Game"); this.addState(new MainMenuState(MAINMENUSTATE)); this.addState(new GameplayState(GAMEPLAYSTATE)); this.addState(new HighscoreState(HIGHSCORESTATE)); 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); } } |
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 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; float playScale = 1; float highscoreScale = 1; float optionsScale = 1; float exitScale = 1; float loginScale = 1; float scaleStep = 0.0001f; MainMenuState(int stateID) { this.stateID = stateID; } @Override public int getID() { return stateID; } public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { 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"); } public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { 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; if( ( mouseX >= 0 && mouseX <= play_button.getWidth()) && ( mouseY >= 90 && mouseY <= 90 + play_button.getHeight()) ){ insidePlay = true; } else if( ( mouseX >= 0 && mouseX <= highscore_button.getWidth()) && ( mouseY >= 196 && mouseY <= 196 + highscore_button.getHeight()) ){ insideHighscore = true; } else if( ( mouseX >= 0 && mouseX <= options_button.getWidth()) && ( mouseY >= 277 && mouseY <= 277 + options_button.getHeight()) ){ insideOptions = true; } else if( ( mouseX >= 0 && mouseX <= quit_button.getWidth()) && ( mouseY >= 358 && mouseY <= 358 + quit_button.getHeight()) ){ insideQuit = true; } 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) ){ } }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) ){ } }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 bkg = null; HighscoreState(int stateID) { this.stateID = stateID; } @Override public int getID() { return stateID; } public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { bkg = new Image("res/bkg.jpg"); } public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { 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(); } } |
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|