Ok, so let’s take a look into the code.
LobbyActivity is the last screen of menu seen by players before battle. When the ‘start battle’ button is pressed, the new activity is started, and it’s AndroidHarness, which wraps and loads the jME game (GameController).
When the game ends, destroy() method in AndroidHarness is called, the game is closed and players are back in the menu checking out summary of the battle (ScoresActivity).
com.spaceadventure.View.LobbyActivity
[java]
startActivity(new Intent(getApplicationContext(), AndroidHarness.class));
finish();
[/java]
com.spaceadventure.View.AndroidHarness
[java]
public class AndroidHarness extends Activity implements …
{
protected String appClass = “com.spaceadventure.Controller.GameController”;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
JmeAndroidSystem.setActivity(this);
if (screenFullScreen) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
if (!screenShowTitle) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
final DataObject data = (DataObject) getLastNonConfigurationInstance();
if (data != null) {
logger.log(Level.INFO, "Using Retained App");
this.app = data.app;
ctx = (OGLESContext) app.getContext();
view = ctx.createView(eglConfigType, eglConfigVerboseLogging);
ctx.setSystemListener(this);
layoutDisplay();
} else {
// Discover the screen reolution
//TODO try to find a better way to get a hand on the resolution
WindowManager wind = this.getWindowManager();
Display disp = wind.getDefaultDisplay();
Log.d("AndroidHarness", "Resolution from Window, width:" + disp.getWidth() + ", height: " + disp.getHeight());
// Create Settings
logger.log(Level.INFO, "Creating settings");
AppSettings settings = new AppSettings(true);
settings.setEmulateMouse(mouseEventsEnabled);
settings.setEmulateMouseFlipAxis(mouseEventsInvertX, mouseEventsInvertY);
settings.setUseJoysticks(joystickEventsEnabled);
settings.setResolution(disp.getWidth(), disp.getHeight());
// Create application instance
try {
if (app == null) {
@SuppressWarnings("unchecked")
Class clazz = (Class) Class.forName(appClass);
app = clazz.newInstance();
}
app.setSettings(settings);
app.start();
ctx = (OGLESContext) app.getContext();
view = ctx.createView(eglConfigType, eglConfigVerboseLogging);
// AndroidHarness wraps the app as a SystemListener.
ctx.setSystemListener(this);
layoutDisplay();
} catch (Exception ex) {
handleError("Class " + appClass + " init failed", ex);
setContentView(new TextView(this));
}
}
}
public void destroy()
{
if (app != null) {
app.destroy();
}
try {
startActivity(new Intent(getApplicationContext(), ScoresActivity.class));
finish();
}
catch(Exception e) {}
}
}
[/java]
com.spaceadventure.Controller.GameController
[java]
public class GameController extends SimpleApplication
{
public GameController()
{
super(new StatsAppState());
instance = this;
}
public static GameController getInstance()
{
if (instance == null) {
synchronized (GameController.class) {
if (instance == null) {
instance = new GameController();
}
}
}
return instance;
}
@Override
public void simpleInitApp()
{
hpBars = new LinkedList();
executor = new ScheduledThreadPoolExecutor(2);
setBackground();
setMapBounds();
setShipControls();
loadSounds();
attachBasesToRootNode();
attachScrapToRootNode(MenuResources.detailsLevel);
enableSFX(false);
setDisplayStatView(false);
setDisplayFps(false);
attachSpaceshipsToRootNode();
setCamera();
setHUD();
performFirstShoot();
height = getCamera().getHeight();
width = getCamera().getWidth();
executor.scheduleAtFixedRate(new CollisionResultsThread(), 15, 30, TimeUnit.MILLISECONDS);
}
public void clearInstance()
{
instance = null;
}
public void endGame()
{
removeControls();
this.inputEnabled=false;
Audio.getInstance().stopGameMusic();
stop();
}
}
[/java]
As for the Nifty - not using it had some major pros for us, but it’s not a matter now. 