Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (290)
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  
  Context switching  (Read 2167 times)
0 Members and 1 Guest are viewing this topic.
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Posted 2011-02-06 13:54:44 »

Basically you have one interface/abstract class (let's call it Context) that has an update method and a render method (plus maybe some other methods).
Then you subclass this interface into different contexts depending on what they are supposed to do (like one context for the main menu, one for the actual in-game and so on).
In your gameloop you hold a reference to the current context (thus you will only run that context's code).

Is this a good strategy for games? I mean it does make the code a lot cleaner and possibly easier to understand (since if you have a class called MainMenuContext then you'll know it will be the main menu).
Offline badlogicgames
« Reply #1 - Posted 2011-02-06 14:24:56 »

Yes, usually those Contexts are called screens. And you can also have subscreens if you want to render overlays and such. At least that's how i do and and how i've seen a lot of other people do it.

http://www.badlogicgames.com - musings on Android and Java game development
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #2 - Posted 2011-02-06 15:30:03 »

Ah, I knew I was onto something. Thanks Smiley
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline JL235

JGO Coder


Medals: 10



« Reply #3 - Posted 2011-02-06 20:09:56 »

They are also known as 'Worlds' in Greenfoot.

Offline zoto

Senior Member


Medals: 4



« Reply #4 - Posted 2011-02-07 21:14:36 »

I have been doing it this way for a couple years now, my interface looks like this.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
import java.awt.Graphics2D;
import java.awt.event.KeyListener;
import javax.swing.event.MouseInputListener;


public interface GameState extends MouseInputListener, KeyListener {
   public void draw(Graphics2D g);
   public void update(long time);
   public GameState nextState();
}

I have the nextState function so state changes are controlled by the current state with a line in your main game class's update function like:
1  
currentState = currentState.nextState();
Offline gouessej

JGO Ninja


Medals: 33
Projects: 1


TUER


« Reply #5 - Posted 2011-02-08 15:28:16 »

You can use a real state machine with the Fettle API as I do.

Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #6 - Posted 2011-02-08 23:34:16 »

I have been doing it this way for a couple years now, my interface looks like this.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
import java.awt.Graphics2D;
import java.awt.event.KeyListener;
import javax.swing.event.MouseInputListener;


public interface GameState extends MouseInputListener, KeyListener {
   public void draw(Graphics2D g);
   public void update(long time);
   public GameState nextState();
}

I have the nextState function so state changes are controlled by the current state with a line in your main game class's update function like:
1  
currentState = currentState.nextState();


You are creating a linked-list of GameStates? Why?

I have a class called GameWorld that acts much like a JPanel.
I add objects of a type that extends GameComponent, which is an abstract class with a draw and update method.
So you have almost the same setup Cheesy

By the way, what is the
1  
long time
parameter in your update method for?

Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 42
Projects: 6



« Reply #7 - Posted 2011-02-09 03:36:22 »


You are creating a linked-list of GameStates? Why?
 

it's not a linked list . Probably this method is called each tick (from the "Main Context Holder"), and when it returns anything different than null, it's time to switch the current context to the one that was returned from the method .

By the way, what is the
1  
long time
parameter in your update method for?

delta time, I suppose .

It's amazing how it looks like my own context switching, which I thought it was very original :p

Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #8 - Posted 2011-02-09 05:05:19 »

Whoa alright. I'm quite confused by what you mean by Context Switching here. Could you explain?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (66 views)
2013-05-17 21:29:12

alaslipknot (75 views)
2013-05-16 21:24:48

gouessej (106 views)
2013-05-16 00:53:38

gouessej (103 views)
2013-05-16 00:17:58

theagentd (113 views)
2013-05-15 15:01:13

theagentd (103 views)
2013-05-15 15:00:54

StreetDoggy (148 views)
2013-05-14 15:56:26

kutucuk (170 views)
2013-05-12 17:10:36

kutucuk (169 views)
2013-05-12 15:36:09

UnluckyDevil (178 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.099 seconds with 22 queries.