Tuesday, March 24, 2009

Adding HUD to Asteroids 3D in JME #11



You now might want to add a HUD to your application so you can display game information to your user. I will show you how to create a simple menu bar at the top of your application using FengGui. FengGui can be used to add components to your application. In my prior post I showed you how to install it.

In your Asteroid3DGame.java class, you should add these as global variables.

private int asteroids=0;
private int ships=0;
private Display disp=null;
private FengJMEInputHandler input=null;
private Label statusbar=null;

We are going to display the number of asteroids and ships you have in the game. The asteroids and ships variable will keep track of the number of asteroids and ships. At each location where you add an asteroid or ship in your game, you should add this variable to increment the counters.

asteroids++;

or

ships++;

At the end of your initGame method, you should add this function call.

initGUI();

In your render(float interpolation) method, you should add this code:

statusbar.setText("Asteroids: "+asteroids+" Ships: "+ships+" ");
disp.display();

This code sets the label in your status bar and renders the FengGui display.

You should add this function to your class. This is the method that will create a label to display in your application using FengGui.

protected void initGUI()
{
disp = new org.fenggui.Display(new org.fenggui.render.lwjgl.LWJGLBinding());
input = new FengJMEInputHandler(disp);
statusbar = new Label("Asteroids: "+asteroids+" Ships: "+ships+" ");
statusbar .setX(20);
statusbar .setY(450);
statusbar .setSizeToMinSize();
statusbar .getAppearance().setTextColor(new Color(100,255,50));
statusbar .setVisible(true);
disp.addWidget(statusbar);
disp.layout();
}

Now when you run your application, you should see a status bar at the top that will display the number of ships and asteroids you have in the game.

(Step #12 Coming Soon)

2 comments:

Anonymous March 24, 2009 9:42 AM  

Thank you, this is exactly what I was looking for!

Traroth November 26, 2010 1:26 AM  

This tutorial is great!