Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (111)
games submitted by our members
Games in WIP (553)
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  
  Init, Render and Update [FIXED]  (Read 1616 times)
0 Members and 1 Guest are viewing this topic.
Offline ShuKen

Junior Member


Medals: 4



« Posted 2012-10-18 19:06:47 »

Hello guys, Im doing some tests and i founded something interesting. Probably Im doing something wrong, but i cant find it.

I have a "test game" with 2 states: MainMenu and GamePlay.

I will copy just the basic of the code:

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  
public class GamePlayState extends BasicGameState {

    int stateID;
    private boolean escape_pressed;
   
    public GamePlayState( int stateID ) {
       this.stateID = stateID;
    }
 
    @Override
    public int getID() {
        return stateID;
    }
 
   
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
     
      escape_pressed= false;
     
      gc.getInput().addKeyListener(listener);
    }
 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
       
       //here i draw things bla bla...
           
             
    }
 
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {      
      if(escape_pressed) sbg.enterState(GameTest08.MAINMENU_STATE);
     
      player.update(delta);

    }//fin update


And the MainMenu

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  
public class MainMenuState extends BasicGameState {

    int stateID;
   
    private Image background;
   
    private long elapsedTime;
    private boolean showHelp;
   
    public MainMenuState(int ID){
       this.stateID= ID;
    }
   
   
    public int getID() {
        return stateID;
    }

   
   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
      background= new Image("data/tests/fondo2.jpg");
      elapsedTime= 0;
      showHelp= false;
     
   }

   
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
      background.draw(0, 0, gc.getWidth(), gc.getHeight());
     
      g.drawString(String.format("blablabla this is the menu"), 500, 400);
     
      if(showHelp) g.drawString(String.format("blablablabla help"), 510, 450);
     
   }

   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
     
      if(gc.getInput().isKeyDown(Input.KEY_ENTER)) sbg.enterState(GameTest08.GAMEPLAY_STATE);
     
      elapsedTime+= delta;
      if(elapsedTime >= 5000) showHelp= true;
     
   }
   
}//fin de clase


The thing is, on GamePlay Im using a class "Escuchador" who implements keyListener. When you are on MainMenu, you press ENTER and GamePlay starts. All good. (Look at "elapsedTime" variable...on init, its 0.)

Now we are on GamePlay. The keyListener works very well and when i press ESCAPE, escape_pressed= true. (Look at the init, escape_pressed= false). On the update, as escape_pressed==true, we go back to the menu.

Again on MainMenu, elapsedTime should be 0. Its not. "help" its visible becouse elpasedTime have the same value has before enter GamePlay. Also, now I press ENTER to go to GamePlay again. Right after entering GamePlayState, I come back to MainMenu, of course, escape_pressed is true. Conclusion: init method does not run when I enter on a new gameState. Is that normal?
Offline ShuKen

Junior Member


Medals: 4



« Reply #1 - Posted 2012-10-18 19:43:51 »

FIXED

I realize that i must implement the "public void enter(...)" method. This one is executed everytime I enter on a new state.

Smiley
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.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
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!