Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 2524 times)
0 Members and 1 Guest are viewing this topic.
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Posted 2011-02-06 12: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 13: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 14:30:03 »

Ah, I knew I was onto something. Thanks Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JL235

JGO Coder


Medals: 10



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

They are also known as 'Worlds' in Greenfoot.

Offline zoto

Senior Member


Medals: 4



« Reply #4 - Posted 2011-02-07 20: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
« Reply #5 - Posted 2011-02-08 14:28:16 »

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

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-02-08 22: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: 48
Projects: 4
Exp: 8 years



« Reply #7 - Posted 2011-02-09 02: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: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-02-09 04: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.

 

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

The first screenshot will be displayed as a thumbnail.

BurntPizza (24 views)
2014-09-19 03:14:18

Dwinin (39 views)
2014-09-12 09:08:26

Norakomi (67 views)
2014-09-10 13:57:51

TehJavaDev (93 views)
2014-09-10 06:39:09

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
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!