AppStates can be used to keep code manageable by keeping code separate in different state classes, such as game and GUI elements.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFrt3Ywj7_C0QrGuWaaZakh-rH9tafzUZqbV7CGniaalhE3V8mAJmb6avfc4HpIMhg__BYBlKUqIR8adegC0xxn31PhxiPxGg-OvsC7GJN8v3q62EyFVJm50OHbHOpAL1M7Sh6RiQ7KWWE/s1600/java41+-+1.png)
Here 3 state classes are written. This is State1.java with some overrides.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3pk-ajjnqdxEBBVyoLvsQEALIBZFCbtF59wZ2Z55TCL1IZYTVHHN1CtfwTxTs81AIY2NXbOK-5a_r_Di-Wi10-ZiRkvJpfxVZD0SPJh12XCrbbwEmVtIVeI9dyfJxtHDJmxoVKOehvGkK/s1600/java41+-+2.png)
// State1.java package mygame; import com.jme3.app.Application; import com.jme3.app.state.AbstractAppState; import com.jme3.app.state.AppStateManager; class State1 extends AbstractAppState{ @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); System.out.println("Initialized - 1"); } @Override public void stateAttached(AppStateManager stateManager) { super.stateAttached(stateManager); System.out.println("Attached - 1"); } @Override public void update(float tpf) { super.update(tpf); System.out.println("update - 1"); } @Override public void stateDetached(AppStateManager stateManager) { super.stateDetached(stateManager); System.out.println("Detached - 1"); } @Override public void cleanup() { super.cleanup(); System.out.println("Cleanup - 1"); } }
This is State2.java with some overrides. There are more overrides available.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijwtr58vbgIArgqHdEox_m8N4JnoTEPe2GWGn04UlQUw2FhpQ1EvQWRvXGGcV8riG1W1wpubaTJObLZ4GD5P9X6ZHY6iC67twiyke218QAhGKiu1TeQz2nsoaKCFLQfCWe_a_qb5UOnrlS/s1600/java41+-+3.png)
// State2.java package mygame; import com.jme3.app.Application; import com.jme3.app.state.AbstractAppState; import com.jme3.app.state.AppStateManager; class State2 extends AbstractAppState{ @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); System.out.println("Initialized - 2"); } @Override public void stateAttached(AppStateManager stateManager) { super.stateAttached(stateManager); System.out.println("Attached - 2"); } @Override public void update(float tpf) { super.update(tpf); System.out.println("update - 2"); } @Override public void stateDetached(AppStateManager stateManager) { super.stateDetached(stateManager); System.out.println("Detached - 2"); } @Override public void cleanup() { super.cleanup(); System.out.println("Cleanup - 2"); } }
This is State3.java with some overrides. These are just skeletons and they might be used in 2D GUI or parts of 3D scene graph.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtrcm7jX_FfacYoblA5VnVqCCY9Y1BTozV15cXmIkr-qa6QmQGh57Mr0ar-uJ64QhKFUt9BaEWB5cZZyV1uUSEaxV2Bob-JOFAuCPwuGr-2PsDo6cOMRvU2qLvIbokHkZkHdj86EKt_2XL/s1600/java41+-+4.png)
// State3.java package mygame; import com.jme3.app.Application; import com.jme3.app.state.AbstractAppState; import com.jme3.app.state.AppStateManager; class State3 extends AbstractAppState{ @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); System.out.println("Initialized - 3"); } @Override public void stateAttached(AppStateManager stateManager) { super.stateAttached(stateManager); System.out.println("Attached - 3"); } @Override public void update(float tpf) { super.update(tpf); System.out.println("update - 3"); } @Override public void stateDetached(AppStateManager stateManager) { super.stateDetached(stateManager); System.out.println("Detached - 3"); } @Override public void cleanup() { super.cleanup(); System.out.println("Cleanup - 3"); } }
We can start states, by attaching them to the stateManager.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwXJjNBgCIsglvOpKzKoygMHG6K8NN6DLVZZCbIKVTo6fI58H9G05rk1oP6m-3T6Fc5ahbjuUQTSRNyLk0G_v3mzQn2fVDjSm8dEXtvMhHSBmbXt8XRsvsdZJo-cwYaeRLuuNu4l-4RA75/s1600/java41+-+5.png)
// *** 1. Start (creating states and starting State1,State2) state1 = new State1(); state2 = new State2(); state3 = new State3(); System.out.println("Attaching state1 and state3."); stateManager.attach(state1); stateManager.attach(state3); // *** 1. End
For this simple example, different states are started and stopped according to this table.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHo33rOwL-UtmEg91goICil9jzKXYBiXCZpQtFAWMMkLOg9iAyw03rZlAg39Dkd5J0_HM43NPZI6wUae5UrHSFi6KP04qX5K_JAoNHHSmFZ1QzBD9-7Q-3LHUIN83EGOz-c53Gn1h82Gwl/s1600/java41+-+6.png)
// *** 2. Start (Starting and ending states) State1 active1 = stateManager.getState(State1.class); State2 active2 = stateManager.getState(State2.class); State3 active3 = stateManager.getState(State3.class); if (active1!=null) { System.out.println("state1 active"); } if (active2!=null) { System.out.println("state2 active"); } if (active3!=null) { System.out.println("state3 active"); } counter++; if (counter<60) { System.out.print("counter: " + counter); System.out.println("\tin SimpleUpdate()"); } if (counter==10) { System.out.println("Detaching state3."); stateManager.detach(stateManager.getState(State3.class)); } else if (counter==20) { System.out.println("Detaching state1."); stateManager.detach(stateManager.getState(State1.class)); System.out.println("Attaching state2."); stateManager.attach(state2); } else if (counter==30) { System.out.println("Attaching state3."); stateManager.attach(state3); } else if (counter==40) { System.out.println("Detaching state2."); stateManager.detach(stateManager.getState(State2.class)); } else if (counter==50) { System.out.println("Detaching state3."); stateManager.detach(stateManager.getState(State3.class)); } // *** 2. End
// JMonkey41.java package mygame; import com.jme3.app.SimpleApplication; public class JMonkey41 extends SimpleApplication { private int counter = 0; private State1 state1; private State2 state2; private State3 state3; public static void main(String[] args) { JMonkey41 app = new JMonkey41(); app.start(); } @Override public void simpleInitApp() { // *** 1. Start (creating states and starting State1,State2) state1 = new State1(); state2 = new State2(); state3 = new State3(); System.out.println("Attaching state1 and state3."); stateManager.attach(state1); stateManager.attach(state3); // *** 1. End } @Override public void simpleUpdate(float tpf) { // *** 2. Start (Starting and ending states) State1 active1 = stateManager.getState(State1.class); State2 active2 = stateManager.getState(State2.class); State3 active3 = stateManager.getState(State3.class); if (active1!=null) { System.out.println("state1 active"); } if (active2!=null) { System.out.println("state2 active"); } if (active3!=null) { System.out.println("state3 active"); } counter++; if (counter<60) { System.out.print("counter: " + counter); System.out.println("\tin SimpleUpdate()"); } if (counter==10) { System.out.println("Detaching state3."); stateManager.detach(stateManager.getState(State3.class)); } else if (counter==20) { System.out.println("Detaching state1."); stateManager.detach(stateManager.getState(State1.class)); System.out.println("Attaching state2."); stateManager.attach(state2); } else if (counter==30) { System.out.println("Attaching state3."); stateManager.attach(state3); } else if (counter==40) { System.out.println("Detaching state2."); stateManager.detach(stateManager.getState(State2.class)); } else if (counter==50) { System.out.println("Detaching state3."); stateManager.detach(stateManager.getState(State3.class)); } // *** 2. End } }
No comments:
Post a Comment