0

Im trying to build a graphic game (in java) that needs to move between worlds when the player reaches a specific point. Each world is represented by a class that extends GCanvas and the whole game is managed from the main class.

I start when the main class presents the first world. When the player reaches a certain point, it changes a variable within this world's class. I want the main class to monitor this variable and switch to the next world when this indicator becomes true. I assume that threads can be helpful, but am not sure how to do it and would like to get some help.

Thats the idea of the main class:

world1 = new GCWorld1();
add(world1);
/**when the player reaches the point (world1.getMoveWorldIndic())*/
remove(world1);
world2=new GCWorld2();
add(world2);

Thanks.

Amit
  • 1
  • 2
  • Have a look at this. This is what you are looking for https://sourcemaking.com/design_patterns/observer – Jabir Nov 22 '15 at 07:54

2 Answers2

0

I would tackle the problem differently. Instead of monitoring a variable from outside in a thread, and thus waste computation power on polling, I would find a way to notify the class outside to transition into a new world. Lookup the observer design pattern to see how to do this.

Faheem Sohail
  • 806
  • 8
  • 21
0

We can use the PropertyChangeListeners for the javabeans and perform needed action as needed. You can take a look at below URL for this:

Property Change Listeners in Java

Hope this helps to your need.

Community
  • 1
  • 1
Pankaj
  • 615
  • 5
  • 9