For your convenience: MenuSwitchProject
This is a test case. Notice how the first menu doesn’t go away. Instead you get stuck with the old and new menu.
[java]
/*
- Copyright © 2009-2010 jMonkeyEngine
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are
- met:
-
-
- Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-
-
- Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-
- Neither the name of ‘jMonkeyEngine’ nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package co.pixelapp.kartex;
import co.pixelapp.ocean.MyProjectedGrid;
import co.pixelapp.ocean.WaterHeightGenerator;
import com.jme3.input.event.TouchEvent.Type;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.bullet.objects.VehicleWheel;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.cinematic.MotionPath;
import com.jme3.cinematic.MotionPathListener;
import com.jme3.cinematic.events.MotionEvent;
import com.jme3.collision.CollisionResults;
import com.jme3.input.KeyInput;
import com.jme3.input.TouchInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.TouchListener;
import com.jme3.input.controls.TouchTrigger;
import com.jme3.input.event.TouchEvent;
import com.jme3.material.MatParam;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Matrix3f;
import com.jme3.math.Quaternion;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.CameraControl;
import com.jme3.scene.shape.Box;
import com.jme3.shadow.BasicShadowRenderer;
import com.jme3.texture.Texture.MagFilter;
import com.jme3.texture.Texture.MinFilter;
import com.jme3.texture.Texture2D;
import com.jme3.util.SkyFactory;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import jme3tools.optimize.GeometryBatchFactory;
public class Kartex extends SimpleApplication
{
float gravity = 9.81f; // Need private identifier.
float gameScale = 0.1f;
WaterHeightGenerator whGEN;
MyProjectedGrid grid;
private MainMenu mainMenuState;
private Hud hudState;
private byte level = 0;
public static void main(String[] args) {
Kartex app = new Kartex();
app.start();
}
@Override
public void simpleInitApp()
{
mainMenuState = new MainMenu();
stateManager.attach(mainMenuState);
//stateManager.detach(mainMenuState);
flyCam.setEnabled(false);
inputManager.setCursorVisible(true);
cam.setFrustumFar(1000f);
//flyCam.setMoveSpeed(10);
}
public void simpleUpdate(float tpf)
{
}
public void startGame()
{
}
public void setLevel(String arg)
{
level = Byte.parseByte(arg);
switch(level)
{
case 1:
stateManager.detach(mainMenuState);
hudState = new Hud();
stateManager.attach(hudState);
break;
}
}
public void quitGame()
{
this.stop();
System.out.println("Pressing quit!");
}
public void quit()
{
}
}
[/java]
[java]
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package co.pixelapp.kartex;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.bullet.BulletAppState;
import com.jme3.input.InputManager;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MainMenu extends AbstractAppState implements ScreenController
{
private Nifty nifty;
private SimpleApplication app;
private Node rootNode;
private AssetManager assetManager;
private AppStateManager stateManager;
private InputManager inputManager;
private ViewPort viewPort;
private BulletAppState physics;
private AudioRenderer audioRenderer;
private Kartex kartapp;
@Override
public void initialize(AppStateManager stateManager, Application app)
{
super.initialize(stateManager, app);
this.app = (SimpleApplication) app; // can cast Application to something more specific
this.rootNode = this.app.getRootNode();
this.assetManager = this.app.getAssetManager();
this.stateManager = this.app.getStateManager();
this.inputManager = this.app.getInputManager();
this.viewPort = this.app.getViewPort();
this.physics = this.stateManager.getState(BulletAppState.class);
this.audioRenderer = this.app.getAudioRenderer();
this.kartapp = (Kartex) app; // can cast Application to something more specific
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
viewPort);
nifty = niftyDisplay.getNifty();
Logger.getLogger("de.lessvoid.nifty").setLevel(Level.SEVERE);
Logger.getLogger("NiftyInputEventHandlingLog").setLevel(Level.SEVERE);
nifty.fromXml("Interface/DesktopGUI.xml", "start", this);
// attach the nifty display to the gui view port as a processor
viewPort.addProcessor(niftyDisplay);
System.out.println("I have been initialized!");
}
@Override
public void update(float tpf)
{
}
@Override
public void stateDetached(AppStateManager stateManager)
{
}
@Override
public void stateAttached(AppStateManager stateManager)
{
}
public void bind(Nifty nifty, Screen screen)
{
}
public void onStartScreen()
{
}
public void onEndScreen()
{
}
public void setLevel(String lv)
{
kartapp.setLevel(lv);
System.out.println("Setting the level!");
}
public void quitGame()
{
app.stop();
}
}
[/java]
[java]
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package co.pixelapp.kartex;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.bullet.BulletAppState;
import com.jme3.input.InputManager;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Hud extends AbstractAppState implements ScreenController
{
private Nifty nifty;
private SimpleApplication app;
private Node rootNode;
private AssetManager assetManager;
private AppStateManager stateManager;
private InputManager inputManager;
private ViewPort viewPort;
private BulletAppState physics;
private AudioRenderer audioRenderer;
@Override
public void initialize(AppStateManager stateManager, Application app)
{
super.initialize(stateManager, app);
this.app = (SimpleApplication) app; // can cast Application to something more specific
this.rootNode = this.app.getRootNode();
this.assetManager = this.app.getAssetManager();
this.stateManager = this.app.getStateManager();
this.inputManager = this.app.getInputManager();
this.viewPort = this.app.getViewPort();
this.physics = this.stateManager.getState(BulletAppState.class);
this.audioRenderer = this.app.getAudioRenderer();
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
viewPort);
nifty = niftyDisplay.getNifty();
Logger.getLogger("de.lessvoid.nifty").setLevel(Level.SEVERE);
Logger.getLogger("NiftyInputEventHandlingLog").setLevel(Level.SEVERE);
nifty.fromXml("Interface/DesktopGUI.xml", "hud", this);
// attach the nifty display to the gui view port as a processor
viewPort.addProcessor(niftyDisplay);
Element lapElement = nifty.getCurrentScreen().findElementByName("lapNumber");
lapElement.getRenderer(TextRenderer.class).setText("Lap:\n 1");
Element positionElement = nifty.getCurrentScreen().findElementByName("positionNumber");
positionElement.getRenderer(TextRenderer.class).setText("Position:\n 4th");
Element speedElement = nifty.getCurrentScreen().findElementByName("speedNumber");
speedElement.getRenderer(TextRenderer.class).setText("Speed:\n 100k/h");
}
@Override
public void update(float tpf)
{
}
@Override
public void stateDetached(AppStateManager stateManager)
{
}
@Override
public void stateAttached(AppStateManager stateManager)
{
}
public void bind(Nifty nifty, Screen screen)
{
}
public void onStartScreen()
{
}
public void onEndScreen()
{
}
}
[/java]
[java]
<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“nifty-default-styles.xml” />
<useControls filename=“nifty-default-controls.xml” />
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- This demo shows a two-screen layout in Nifty’s XML syntax. -->
<!-- You see two screens with two layers each, contain several panels. -->
<!-- The panels contain images, text, and controls (label and buttons). -->
<!-- Buttons have an interaction defined, and some of the text -->
<!-- is dynamically defined, using the MyStartScreen controller. -->
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- +++++++++++++++++++++++++++++++++++++++ -->
<!-- lay out the start screen/layers/panels -->
<!-- +++++++++++++++++++++++++++++++++++++++ -->
<screen id=“start” controller=“co.pixelapp.kartex.MainMenu”>
<layer id=“background” childLayout=“center”>
<image filename=“Textures/Kartexcard.jpg”></image>
</layer>
<layer id=“foreground” childLayout=“vertical”>
<panel id=“panel_top” height=“25%” width=“75%” align=“center” childLayout=“center”>
</panel>
<panel id=“panel_mid” height=“50%” width=“75%” align=“center” childLayout=“center”>
</panel>
<panel id="panel_bottom" height="25%" width="75%" align="center" childLayout="horizontal">
<panel id="panel_bottom_left" height="50%" width="50%" valign="center" childLayout="center">
<control name="button" label="Start" id="StartButton" align="center" valign="center"
visibleToMouse="true">
<interact onClick="setLevel(1)" />
</control>
</panel>
<panel id="panel_bottom_right" height="50%" width="50%" valign="center" childLayout="center">
<control name="button" label="Quit" id="QuitButton" align="center" valign="center"
visibleToMouse="true" >
<interact onClick="quitGame()"/>
</control>
</panel>
</panel>
</layer>
</screen>
<!-- +++++++++++++++++++++++++++++++++++++++ -->
<!-- lay out the HUD screen/layers/panels -->
<!-- +++++++++++++++++++++++++++++++++++++++ -->
<screen id=“hud” controller=“co.pixelapp.kartex.Hud”>
<layer id=“background” childLayout=“center”>
<image filename=“Interface/tutorial/hud-frame.png”></image>
</layer>
<layer id=“foreground” childLayout=“vertical”>
<panel id=“panel_top” height=“25%” width=“75%” align=“center” childLayout=“horizontal”>
<panel id=“panel_bottom_left” height=“50%” width=“33%” valign=“center” childLayout=“center”>
<control name=“label” id=“lapNumber” font=“Interface/Fonts/BioDiscTitle.fnt”
color="#000" text=“Lap” width=“100%” height=“100%” />
</panel>
<panel id=“panel_bottom_center” height=“50%” width=“33%” valign=“center” childLayout=“center”>
<control name=“label” id=“positionNumber” font=“Interface/Fonts/BioDiscTitle.fnt”
color="#000" text=“Poston:” width=“100%” height=“100%” />
</panel>
<panel id=“panel_bottom_right” height=“50%” width=“33%” valign=“center” childLayout=“center”>
<control name=“label” id=“speedNumber” font=“Interface/Fonts/BioDiscTitle.fnt” color="#000" text=“Speed:” width=“100%” height=“100%” />
</panel>
</panel>
<panel id=“panel_bottom” height=“75%” width=“75%” align=“center” childLayout=“center”>
</panel>
</layer>
<!–
<layer id=“foreground” childLayout=“horizontal”>
<panel id=“panel_left” width=“80%” height=“100%” childLayout=“vertical” >
<control name=“label” id=“positionNumber” font=“Interface/Fonts/BioDiscTitle.fnt”
color="#000" text=“1st” width=“100%” height=“100%” />
</panel>
<panel id="panel_right" width="20%" height="100%" childLayout="vertical" >
<panel id="panel_top_right1" width="100%" height="15%" childLayout="center" >
</panel>
<panel id="panel_top_right2" width="100%" height="15%" childLayout="center" >
</panel>
<panel id="panel_bot_right" width="100%" height="70%" valign="center" >
</panel>
</panel>
</layer>
-->
</screen>
</nifty>
[/java]
kartexlogo
Hudimage