Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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  
  Passing events back to Game  (Read 473 times)
0 Members and 1 Guest are viewing this topic.
Offline Torquai

Innocent Bystander





« Posted 2013-02-14 22:27:36 »

Im writing my first java game, and I use a main Game class that use different Screens (ie. mapScreen, combatScreen, menuScreen) to handle different sections on the game. And right now I need my mapScreen to tell my game that it's done and we should move to the combatScreen.

And so I just wanted to ask about best practices here, should mapScreen implement ActionListener and have the Game subscribe to these notifications, or is there a more common way to do it?
Also, can anyone please link to a good explenation of how I should implement the ActionListener?

thanks in advance.

-Torquai
Offline ctomni231

JGO Ninja


Medals: 71
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #1 - Posted 2013-02-15 03:03:58 »

Well, for Screens, it isn't a good idea to use Listeners at all actually. The best way to handles Screens is by using simple states. If you have a lot of screens, for instance, and you want to move from one screen to another, you can have an ID that tells your program which Screen to display next. Each Screen can be part of an array, and you can choose which Screen to display based on the ID.

It is a lot more simple than Listeners. I've only found Listeners extremely useful when listening for items on other threads, such as input devices, music thread lines, or Swing buttons.

Offline actual

JGO Coder


Medals: 24



« Reply #2 - Posted 2013-02-15 06:09:47 »

If you don't have that many screens, you could instantiate each one as public fields in Game. (if you want you can write methods like getMapScreen() and getMenuScreen()). If each screen holds a reference to Game, then you can have a simple method in Game like.

1  
2  
3  
public void setScreen(GameScreen screen) {
   currentScreen = screen;
}


Then within your mapScreen you could just call game.setScreen(game.combatScreen);. You can make it a bit more sophisticated too. For instance if each screen has code that it needs run only when it's initialized or when it's closed, you can modify your setScreen code to somehting like:

1  
2  
3  
4  
5  
public void setScreen(GameScreen screen) {
   currentScreen.close();
   currentScreen = screen;
   currentScreen.initialize();
}


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zngga
« Reply #3 - Posted 2013-02-16 15:55:38 »

I normally will create a ScreenManager for this kinda stuff. Your game would have the ScreenManager, and all your screens would be in an list or map of some kind within ScreenManager. All your screens would have a reference to ScreenManager and when you need to switch it may look something like this:
1  
ScreenManager.switchScreen("ScreenID");


the switch method in the manager might look like this:
1  
2  
3  
4  
5  
6  
public void switchScreen(String id)
{
currentScreen.dispose();
currentScreen = //find screen with id
currentScreen.enter();
}

My code never has bugs... it just develops unexpected features!
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.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (11 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (108 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!