Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (773)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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  
  How to correctly load a state? [SOLVED]  (Read 1474 times)
0 Members and 1 Guest are viewing this topic.
Offline flameomega

Senior Newbie





« Posted 2013-05-30 04:12:20 »

So game progress has been going quite nicely:

<a href="http://www.youtube.com/v/LzLgQsTy0V0?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/LzLgQsTy0V0?version=3&amp;hl=en_US&amp;start=</a>

But I've noticed something -- the actual game's backgroundmusic and the heartbeat sound plays in the menu state.

So I've tried to make all the stuff for the actual game (such as the backgroundmusic and the heartbeat) init only if the game is in the play(actual game) state (which is 2).

Play.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
if(sbg.getCurrentStateID() == 2){
         
         // Images
         map = new Image("resources/Images/Backgrounds/backGround_two.png");

         // Music
         backGroundMusic = new Sound("resources/Music/BGM/eerie.wav");
         backGroundMusic.loop();
         // HUD BLOCK START
         hudGUI = new HUD();

         healthGUI = new Health();
         healthGUI.init(gc);

         inventoryGUI = new Inventory();
         inventoryGUI.init(gc);
         // HUD BLOCK END

         mainPlayer.getWeapon().init(gc);
         
      }


But that doesn't work.

So I've made a breakpoint in the Menu's update method, and a breakpoint for that if statement in the play class -- debugged -- the play class' if statement is met first
and THEN the update method is met.

Quite strange.

Here's how the "main game" (not the "actual game state", which is Play.java, sorry if all these names are confusing :S) class is setup:

Game.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public void initStatesList(GameContainer container) throws SlickException {

      // 0
      ResourcesState rs = new ResourcesState();
      states.add(rs);
      addState(rs);

      // 1
      Menu menu = new Menu();
      states.add(menu);
      addState(menu);
     
      // 2
      Play play = new Play();
      states.add(play);
      addState(play);
     
      // 3
      Credit credits = new Credit();
      states.add(credits);
      addState(credits);
     
   }


Any suggestions?
Offline Troncoso

JGO Coder


Medals: 20



« Reply #1 - Posted 2013-05-30 05:39:22 »

I'd place the backGroundMusic.loop in the enter() method, and put some sort of pause in the leave() method of the play state.


A little more of your code would be useful, though.
Offline flameomega

Senior Newbie





« Reply #2 - Posted 2013-05-30 17:35:00 »

There is no "enter()" method nor is there a "leave()" method.

Unless you're referring to enter() as the init() method.

If so, it is in that method.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Troncoso

JGO Coder


Medals: 20



« Reply #3 - Posted 2013-05-30 19:08:24 »

From the looks of it, you are using Slick2D's StateBasedGame approach. If that's the case, the BasicGameState class has two classes:

enter(GameContainer gc, StateBasedGame sbg) throws SlickException
leave(GameContainer gc, StateBasedGame sbg) throws SlickException

that can optionally be overwritten. As the names suggest, enter runs when a state is transitioned into, and leave runs when exiting the state.
Offline flameomega

Senior Newbie





« Reply #4 - Posted 2013-05-31 02:03:49 »

Oh awesome! Thanks, I'll be sure to reply with my results Smiley
Offline flameomega

Senior Newbie





« Reply #5 - Posted 2013-06-01 01:13:27 »

From the looks of it, you are using Slick2D's StateBasedGame approach. If that's the case, the BasicGameState class has two classes:

enter(GameContainer gc, StateBasedGame sbg) throws SlickException
leave(GameContainer gc, StateBasedGame sbg) throws SlickException

that can optionally be overwritten. As the names suggest, enter runs when a state is transitioned into, and leave runs when exiting the state.

I did
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  
public void enter(GameContainer gc, StateBasedGame sbg) throws SlickException {
     
      // Images
      map = new Image("resources/Maps/chinatown.png");
      tileManager = new TileManager(map);
      tileManager.addSolidTile(0, 0);

      // Music
      backGroundMusic = new Sound("resources/Music/BGM/eerie.wav");
      backGroundMusic.loop();
      // HUD BLOCK START
      hudGUI = new HUD();

      healthGUI = new Health();
      healthGUI.init(gc);

      inventoryGUI = new Inventory();
      inventoryGUI.init(gc);
      // HUD BLOCK END

      mainPlayer.getWeapon().init(gc);
      magnum = new Magnum(null);
      magnum.init(gc);
      assault = new LE9AR(null);
      assault.init(gc);
     
   }


and it works!

Only plays the Play's music and the heartbeat sound once you go ingame!

Thanks a lot man Smiley
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1850 views)
2018-06-10 19:43:48

EgonOlsen (1879 views)
2018-06-10 19:43:44

EgonOlsen (1245 views)
2018-06-10 19:43:20

DesertCoockie (1680 views)
2018-05-13 18:23:11

nelsongames (1345 views)
2018-04-24 18:15:36

nelsongames (1963 views)
2018-04-24 18:14:32

ivj94 (2736 views)
2018-03-24 14:47:39

ivj94 (1938 views)
2018-03-24 14:46:31

ivj94 (3029 views)
2018-03-24 14:43:53

Solater (1078 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!