Hi, i’m just starting to try game development using java
I already know the basic stuff like the collections, arrays, objects, classes, simple gui forms
I started gathering all the information i can get my hands on and started trying out easy coding examples. What i’m trying to create is a tilemap with predefined pictures, turn based, keybord input, 2 players.
I’m trying to use MIDP 2.0 because it includes a Game API and a layerManager wich i think is just what i need.
So i tried following this tutorial at http://developers.sun.com/techtopics/mobility/midp/articles/game/
But it gives an error when i try to run it.
I did the basic game loop with a rotating X that can be controlled by the keybord…
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class SimpleGameCanvas
extends GameCanvas
implements Runnable {
private volatile boolean mTrucking;
private long mFrameDelay;
private int mX, mY;
private int mState;
public SimpleGameCanvas() {
super(true);
mX = getWidth() / 2;
mY = getHeight() / 2;
mState = 0;
mFrameDelay = 20;
}
public void start() {
mTrucking = true;
Thread t = new Thread(this);
t.start();
}
public void stop() { mTrucking = false; }
public void run() {
Graphics g = getGraphics();
while (mTrucking == true) {
tick();
input();
render(g);
try { Thread.sleep(mFrameDelay); }
catch (InterruptedException ie) { stop(); }
}
}
private void tick() {
mState = (mState + 1) % 20;
}
private void input() {
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0)
mX = Math.max(0, mX - 1);
if ((keyStates & RIGHT_PRESSED) != 0)
mX = Math.min(getWidth(), mX + 1);
if ((keyStates & UP_PRESSED) != 0)
mY = Math.max(0, mY - 1);
if ((keyStates & DOWN_PRESSED) != 0)
mY = Math.min(getHeight(), mY + 1);
}
private void render(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
g.drawLine(mX, mY, mX - 10 + mState, mY - 10);
g.drawLine(mX, mY, mX + 10, mY - 10 + mState);
g.drawLine(mX, mY, mX + 10 - mState, mY + 10);
g.drawLine(mX, mY, mX - 10, mY + 10 - mState);
flushGraphics();
}
}
I transferred the classes to the java dir and added them into the program.
But it gives an error at g.setColor(0xffffff);
g is null hence the java.lang.nullpointer exeption… how can a example be wrong, or is this only made for cellphones?
If i’m realy wrong what classes should i use then to create a tile map, letting 2d pictures move around it and turn based?