Basic MIDP 2.0 Game Template Part 3

Jump to part: 1 | 2 | 3

Displaying the GameCanvas

In this last part of the tutorial entitled Basic Game Template, we will modify the code of our MIDlet and Canvas classes to make the both ends meet and the application to run. It is recommended that you start with Part 1 of this tutorial if you haven't done so.

First, we'll need to add a new global variable to reference the MIDlet from the canvas class. Let's call it fParent. So add this code to clsCanvas like so:

private Graphics g;
private midMain fParent;



Modify the contructor to accept the MIDlet class as a parameter and assign the value to our global variable:

public clsCanvas(midMain m) {
super(true);
fParent = m;
}


The canvas needs a way to notify the MIDlet that the main loop has ended and the program needs to terminate. Add this code at the end of our run() method:

}
g = null;
fParent.destroyApp(false);
fParent = null;

}



We need a new method that the MIDlet can call to start the game thread. Let's call it the start() method and add the code under the clsCanvas constructor:


public void start(){
Thread runner = new Thread(this);
runner.start();
}



You should press Shift+ALT+F after editing the code...just in case.

Here's the completed clsCanvas source code:

package MyGame;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

public class clsCanvas extends GameCanvas implements Runnable {
private boolean isRunning = true;
private Graphics g;
private midMain fParent;

public clsCanvas(midMain m) {
super(true);
fParent = m;
}

public void start(){
Thread runner = new Thread(this);
runner.start();
}


public void run() {
int iKey = 0;
g = getGraphics();
while(isRunning){

iKey = getKeyStates();

if ((iKey & GameCanvas.FIRE_PRESSED) != 0){
isRunning = false;
}

//set drawing color to black
g.setColor(0x000000);
//fill the whole screen
g.fillRect(0, 0, getWidth(), getHeight());
// set drawing color to white
g.setColor(0xffffff);
//display the key code last pressed
g.drawString(Integer.toString(iKey), 2, 2, Graphics.TOP | Graphics.LEFT);
flushGraphics();

try{
Thread.sleep(30);
} catch (Exception ex){

}
}
g = null;
fParent.destroyApp(false);
fParent = null;

}
}



Now open the source code of our MIDlet. We will define a new global variable called myCanvas that will allow the MIDlet to create and reference the clsCanvas class. Place the code just under the midMain class declaration:

public class midMain extends MIDlet {
clsCanvas myCanvas;



Next, we will modify the startApp() method of our midlet so that it creates a new instance of the clsCanvas class, starts it in a new thread, and finally making the the canvas the current displayed item. Add this code to the startApp() method:

public void startApp() {
Display d = Display.getDisplay(this);
myCanvas = new clsCanvas(this);
myCanvas.start();
d.setCurrent(myCanvas);

}



The last code we will add is for making sure we release all the resources our MIDlet has used and terminate gracefully when your game has ended. Place this code in the destroyApp() method:

public void destroyApp(boolean unconditional) {
myCanvas = null;
notifyDestroyed();

}



Here's the completed midMain source code:

package MyGame;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class midMain extends MIDlet {
clsCanvas myCanvas;

public void startApp() {
Display d = Display.getDisplay(this);
myCanvas = new clsCanvas(this);
myCanvas.start();
d.setCurrent(myCanvas);

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
myCanvas = null;
notifyDestroyed();

}
}



Yay! We're done! You can now test your by pressing the F6 key on your keyboard or clicking on the Run Main Project icon on the toolbar. That ends our tutorial on creating a basic template for your game. I will also be using this template for the rest of the tutorials that will be posted here. If you have some comments, suggestions or having problem with the source code, feel free to leave a comment or use the ShoutBox. Have fun.


5 comments   |   post a comment
said...
I now have the basic idea, Now I guess learning the concepts of Java would be the next step? Thanks for the tutorial :)
said...
You're very welcome :).

Your right! I started making games without knowing much about Java too. A little bit about Javascript and ActionScript but that's about it. You just have to know the basics like how to handle arrays, what basic data types are available, and whats the syntax for loops and conditional statements. Learning about Vectors and classes will help too.

But I guess when you reach the tutorials about Images and clipping, you will be able to make games already.

Have fun!!
said...
Hi devlin. i just started with mobile game programming. i havnt done anything yet really.ive set myself a deadline of getting one done in 2 mnths at most.Its still a little fuzzy for me sometimes but im getting the hang of it.i was wondering if you could point me to a tutorial on how to use the new visual game builder tool in netbeans 6.1. i intend to try my hands at that after i finish your tutorials. i am also reading michael morrison's book; beginning mobile phone game programming and hope to beat my personal deadlien i set. thanks for the tutorials, great tutorials.
said...
Hi dee,

Here's one tutorial from the NetBeans Community Docs wiki:
http://wiki.netbeans.org/CreatingJavaMEGamesWithGameBuilder
said...
Hi Devlin,
I was wondering lets say I'd like the option to pauze the canvas... for example I have a Menu which has a Play option, when selected the cnavas will be shown, but also on the canvas itself there is an option to go back to the menu, if this is selected it seems to me it would be wise to pause the canvas so it doesnt take processing power... Is the only way to set isRunning variable to false ? And if that is reset to true (like a resume() method that does that) would that be enough to resume the canvas ? Thx in advance :D