Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 23:35, 1 February 2008.

How to develop a game - Part 1

From Forum Nokia Wiki


So you want to develop for your mobile phone? Say hello to Java Micro Edition. JavaME combines a small JVM and a set of Java APIs for developing applications for mobile devices.

This article is the first in a series where we are going to approach all the basics for developing an application for mobile phones using JavaME. The final goal of these tutorials is to develop a small Arkanoid clone (http://en.wikipedia.org/wiki/Arkanoid) and in the process learn all the base libraries, classes and methods available in JavaME:

This time, after a quick introduction to the development tools, I will provide a step-by-step guide to creating your first JavaME application, also known as a MIDlet.

Development Tools

The development environment you use has a big influence on your productivity.

After trying out a lot of different IDEs, I narrowed my options to the following two:

Both IDEs are completely free and have a strong community backing them up. They allow you to do all the work you need to develop mobile applications.

Netbeans is very easy to install and configure and its interface is more intuitive than Eclipse, but it uses a lot of memory and when your project gets bigger it can be very slow.

Eclipse is a lot faster and has a smaller memory footprint, it has the same features as Netbeans, but its interface is more awkward to use and it requires more work to configure correctly.

So, what's the best for you? If you already have experience with Eclipse, its smaller memory footprint and lightning speed are a big advantage, but if you are a beginner with Java IDEs and mobile programming, Netbeans is a lot easier to start with.

Phone SDKs

Besides the Java IDE with mobility support you choose, you need to download and install some special software called the phone SDK that allows you to have access to the specific J2ME libraries.

Sun has an emulator called Wireless Toolkit (WTK) that provides all the standard libraries. I regularly use this one for my own development because it's very fast, has a lot of extra tools ( Network Monitor, Profiler, SMS Emulator etc...) and can be customized to look and act like several phones.

Besides the WTK, it's also a good idea to have some SDKs from the manufacturers of the phones that you wish to support. These SDKs allow you to access any special libraries that those phones might use and often include software that allows you to deploy your applications to the phone.

All the big brands of cellular phones have their developer sites where you can download their phone emulators and custom SDKs.

Your First Midlet

After you installed and configured your Java IDE and phone emulator, it's time to start coding!

All the J2ME applications must extend the abstract Midlet class found in the javax.microedition.midlet package, much like creating an applet by extending the java.applet.Applet class. The base entry point is the startApp() method.

So lets create our first Midlet named MyMidlet.

public class MyMidlet extends MIDlet{
  // it's invoked when the application starts and each time is resumed
  protected void startApp() throws MIDletStateChangeException {}
 
  // it's invoked when the Midlet needs to be destroyed
  protected void destroyApp(boolean uncondicional) throws MIDletStateChangeException {}
 
  // it's invoked when the Midlet needs to be paused. (Some phones ignore pauseApp().)
  protected void pauseApp() {}
}

If you create this class and run it in the emulator you will have a fantastic blank screen!

In order to show some content in your application you need to use the Display class, this class controls what appears on the screen of the Midlet. Each Midlet has one Display and can access it through the Display static method getDisplay().

To present something on the screen you need to set a Displayable object using the setCurrent() method. One class that implements Displayable is the Alert class. This class shows a simple message on the screen.

With this information lets change our startApp() method to something more useful.

Alert alert;
 
//entry point for the application
protected void startApp() throws MIDletStateChangeException {
 
  // creates alert
  alert = new Alert("Hello World");
 
  // shows alert in the screen.
  Display.getDisplay(this).setCurrent(alert);
}

If you run your Midlet now, you will see a screen with "Hello World" like the one below:

Image:HelloScreenShot.png

That's a little better but now you still don't have a way to exit your Midlet properly. (You can always press the red button!) In order to have a proper exit, let's add some more lines of code to our startApp() method:

// create command
comExit = new Command("Exit", Command.EXIT, 1);
// add a command to exit the Midlet
alert.addCommand(comExit);

This shows you the exit command on the screen but if you click on it, it doesn't do anything because you need to add a CommandListener to your Alert in order to react to Command Actions. So let's add some more code:

public class MyMidlet extends MIDlet implements CommandListener{
 
  public void startApp(){
    [...]
    // adds a listener to the alert
    alert.setCommandListener(this);
  }
 
  public void commandAction(Command cmd, Displayable display) {
    // check what command was selected
    if (cmd == comExit) {
      notifyDestroyed();
    }
  }
}

With this change we finally managed to have a complete functional Midlet. Yeah!!

In the next lesson, we are going to look in more detail at the user interface elements like the Alert, Display and CommandListener classes. We are going to use those elements to define the menu interface for our Arkanoid clone. See you soon.

Downloads:

Related Discussions
Thread Thread Starter Forum Replies Last Post
making your game available. smb101 Mobile Java General 0 2003-12-24 10:32
downloading game er_rajiv Mobile Java General 0 2004-01-29 16:19
Making my game available for download smb101 Mobile Java General 3 2004-06-24 14:00
3D game drrpbs Mobile Java General 5 2006-02-06 12:34
3650 and SIS download - cant uninstall rixis General Symbian C++ 4 2004-02-03 09:34
 
Powered by MediaWiki