4/19/07
Hi,
I want to thank you very much for the prompt respond to my email.
You are correct.The program I sent you does work.I sent you the wrong one.
I hope you can help me?
I have been working with j2me mobile phone code for just a few months now and had done a few very simple game examples from a book I am using.
The game I am working on now is more complex for me and I am asking for some help on it.
Game description;
It is a game where the character goes through a maze and if the character collides with the walls there is tone and 10 points are subtracted from the score(you start with a 100 points) that is displayed while the game is played.
I did not use the RMS(Record Management System) because I wanted to keep it very simple and not list many score at the same time on display.
The problem I am having is in the Sun Emulator build process where these errors are displayed.
Project "SavethePrincess1Canvas" loaded
Project settings saved
Building "SavethePrincess1Canvas"
D:\java VM\apps\SavethePrincess1Canvas\src\SavethePrincess 1Canvas.java:231: illegal start of
expression
private void paint(Graphics g) {
^
D:\java VM\apps\SavethePrincess1Canvas\src\SavethePrincess 1Canvas.java:251: ';' expected
^
2 errors
com.sun.kvem.ktools.ExecutionException
Build failed
Here is my complete code;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class SavethePrincess1Canvas extends GameCanvas implements Runnable {
private Display display;
private boolean sleeping;
private long frameDelay;
private int inputDelay;
private LayerManager layers;
private int xView, yView;
private TiledLayer BackgroundMapLayer;
private TiledLayer BarrierMapLayer;
private Image InfoBar;
private Sprite General;
private Player musicPlayer;
private int Score;
public SavethePrincess1Canvas(Display d) {
super(true);
display = d;
// Set the frame rate (30 fps)
frameDelay = 33;
// Clear the input delay
inputDelay = 0;
}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
//Initialize the game variables
Score = 100;
// Create the BackgroundMap and BarrierMap tiled layers
try {
infoBar = Image.createImage("/InfoBar.png");
BackgroundMapLayer = new TiledLayer(18,19,Image.createImage("/BackgroundMap.png"),10,10);
BarrierMapLayer = new TiledLayer(18,19, Image.createImage("/BarrierMap.png"),10,10);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Setup the Background tiled layer map
int[] BackgroundMap = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162,
163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198,
199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216,
217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234,
235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252,
253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270,
271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288,
289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306,
307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324,
325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342,
};
for (int i = 0; i < BackgroundMap.length; i++) {
int column = i % 18;
int row = (i - column) / 19;
BackgroundMapLayer.setCell(column, row, BackgroundMap[i]);
}
// Setup the BarrierMap tiled layer map
int[] BarrierMap = {
1, 0, 3, 4, 0, 6, 7, 8, 9, 0, 11, 12, 13, 14, 15, 16, 0, 18,

};
for (int i = 0; i < BarrierMap.length; i++) {
int column = i % 18;
int row = (i - column) / 19;
BarrierMapLayer.setCell(column, row, BarrierMap[i]);
}
// Initialize the General
try {
General = new Sprite(Image.createImage("/General.png"), 6, 7);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Create the layer manager
layers = new LayerManager();
layers.append(General);
layers.append(BarrierMapLayer);
layers.append(BackgroundMapLayer);
xView = (BackgroundMapLayer.getWidth() - getWidth()) / 2;
yView = (BackgroundMapLayer.getHeight() - getHeight()) / 2;
layers.setViewWindow(xView, yView, getWidth(),getHeight());
General.setPosition(xView+(getWidth()-General.getWidth()) /2,
yView + (getHeight() - General.getHeight()) / 1);
// Initialize and start the music player
try {
InputStream is = getClass().getResourceAsStream("Collide.wav");
musicPlayer = Manager.createPlayer(is, "audio/x wav");
musicPlayer.prefetch();
musicPlayer.setLoopCount(1);
musicPlayer.start();
}
catch (IOException ioe) {
}
catch (MediaException me) {
}
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
// Close the music player
musicPlayer.close();
}
public void run() {
Graphics g = getGraphics();
// The main game loop
while (!sleeping) {
update();
draw(g);
try {
Thread.sleep(frameDelay);
}
catch (InterruptedException ie) {}
}
}
private void update() {
// Process user input to move the General
if (++inputDelay > 0) {
int keyState = getKeyStates();
int xMove = 0, yMove = 0;
if ((keyState & LEFT_PRESSED) != 0)
xMove = -1;
else if ((keyState & RIGHT_PRESSED) != 0)
xMove = 1;
if ((keyState & UP_PRESSED) != 0)
yMove = -1;
else if ((keyState & DOWN_PRESSED) != 0)
yMove = 1;
if (xMove != 0 || yMove != 0) {
layers.setViewWindow(xView + xMove, yView + yMove, getWidth(),
getHeight() - InfoBar.getHeight());
General.move(xMove, yMove);
}
// Check for a collision with the General and the BarrierMap tiled layer
if (General.collidesWith(BarrierMapLayer[i], true)) {
// Play a collision sound
try {
Manager.playTone(ToneControl.C4 + 2, 100, 100);
}
catch (Exception e) {
}
//Decrease the General Lives
Score -= 10;
// Restore the original view window and General positions
layers.setViewWindow(xView, yView, getWidth(),
getHeight() - InfoBar.getHeight());
General.move(-xMove, -yMove);
}
else {
// If there is no collision,commit the changes to the view window position
xView += xMove;
yView += yMove;
}
for (int i = 0; i > 10; i++) {
// Update the General and the BarrierMap
General.update();
Barrier[i].update();
// Reset the input delay
inputDelay = 0;
}
private void paint(Graphics g) {
// Draw the InfoBar and numLives
g.drawImage(InfoBar, 0, 0, Graphics.TOP | Graphics.LEFT);
g.setColor(0, 0, 0); // black
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
g.drawString("Score:", 88, 1, Graphics.TOP | Graphics.LEFT);
for (int i = 0; i > Score; i++)
g.setColor(255, 255, 255); // white
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
g.fillRect(40, 3, numLIves, 12);
// Draw the layers
layers.paint(g, 0, InfoBar.getHeight());
// Flush the offscreen graphics buffer
flushGraphics();
}
I had to shorten the BarrierMap program in order for it to fit in the email.
I want to add Score to my game and for the pass several weeks I have tried to analogize the
errors but I am having a difficult time understanding it.
I would greatly appreciate if you could give me some suggestions on how to correct this.
Thank you,
David Brown