I'm having problems trying to get my sprite to move around my tile layer. For some reason, I'm getting the nagging suspicsion that it might be due to the fact that my keypresses are not invoking the methods that move the sprite around. I'm not sure about this, but is it feasible to use both keystates and keypresses at th3e same time and would that help in invoking the sprite movement methods? Also, since my player sprite is five frames (one for standing still and the rest for moving in a given direction), how can I juggle frame sequences when moving and standing still. In other words, when I move my character, will I have to set the frame sequence for those movement methods and for standing still, will I have to create a method that uses a boolean to indicate whether or not the character is moving so that it can be set back to the first frame? Enclosed is the code for my game screen and player sprite.
EDIT: I got it working and I realized why there were movement issues when running my midlet in my phone's default J2ME emulator. It seems that it only implements the basic stuff and only implemented the Game API sparingly. That and after reading up more about this issue, it seems that it is not suited for gaming, thus it has a lot of compatibility issues with lots of games. Luckily, the IBM J2ME Midlet launcher FULLY implements J2ME w/o cutting corners and managed to move my sprite across my tile layer just like in the emulator. Granted it lags a little, but a toning down of my graphics should do the trick.
my game screen class (update 3/29/06)package de.enough.polish.example;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class GameScreen extends GameCanvas implements Runnable, CommandListener{
static final int MILLIS_PER_TICK = 50;
private static final int RUN_OFF = 0;
private Command backCommand = new Command("Back", Command.BACK,1);
private MenuMidlet midlet;
private LayerManager layerManager;
private Thread gameThread;
private TiledLayer background;
private int keyPress;
private int regGameCodes;
private TheLevels theLevels;
private Display display;
private int width;
private int height;
private int screenViewWidth;
private int screenViewHeight;
private int scnX, scnY;
private Image cityBackground;
boolean isPlay;
private PlayerSprite playerSprite;
private Sprite backgroundSprite;
private int currentFrame = 1;
private static final int MOVE_RATE = 5;
private static final int END_TILELAYER_BUFFER = 15;
private static final int OFFSET_LEFT = 30;
private static final int OFFSET_RIGHT = 120;
public GameScreen(MenuMidlet midlet) throws Exception {
super(false);
this.midlet=midlet;
addCommand(backCommand);
setCommandListener(this);
isPlay = true;
theLevels = new TheLevels(screenViewHeight);
background = theLevels.getBackground();
width=getWidth();
height=getHeight();
screenViewHeight = width;
scnX=0;
Image playerImage = Image.createImage("47;sprite.png");
playerSprite = new PlayerSprite(playerImage,24,56,width,height);
playerSprite.startPosition();
layerManager = new LayerManager();
layerManager.append(playerSprite);
layerManager.append(background);
}
public void start() {
gameThread = new Thread(this);
gameThread.start();
try {
cityBackground = Image.createImage("47;citybg.png");
}
catch (Exception e){
}
System.out.print("Game thread running");
}
public void stop() {
gameThread = null;
}
public void run() {
Graphics g = getGraphics();
Thread currentThread = Thread.currentThread();
try{
while (currentThread == gameThread) {
long startTime = System.currentTimeMillis();
if (isShown()) {
if (isPlay) {
switch(regGameCodes){
case UP: playerSprite.moveUp(); break;
case DOWN: playerSprite.moveDown(); break;
case LEFT:
playerSprite.moveLeft();
if ((scnX > 0)&& (playerSprite.getX() < (scnX*MOVE_RATE)+OFFSET_LEFT))
scnX--;
break;
case RIGHT:
playerSprite.moveRight();
if ((scnX < getWidth()+END_TILELAYER_BUFFER)&&(playerSprite.getX() > (scnX*MOVE_RATE)+OFFSET_RIGHT))
scnX++;
break;
}
System.out.println("ScnX: "+scnX*5+". "+"PlayerXPosition: "+playerSprite.getX());
System.out.println("Current width: "+getWidth()+". "+"Tile width: "+theLevels.getWidth());
}
drawScreen(g);
}
long timeTake = System.currentTimeMillis() - startTime;
if (timeTake < MILLIS_PER_TICK) {
synchronized (this) {
wait(MILLIS_PER_TICK - timeTake);
}
} else {
currentThread.yield();
}
}
} catch(Exception ex){
}
}
protected void keyPressed(int keyCode){
super.keyPressed(keyCode);
regGameCodes=getGameAction(keyCode);
keyPress=keyCode;
if ((regGameCodes == UP) || (regGameCodes == DOWN) || (regGameCodes == LEFT) || (regGameCodes == RIGHT) || (keyPress == KEY_NUM1) || (keyPress == KEY_NUM3) || (keyPress == KEY_NUM7) || (keyPress == KEY_NUM9)){
playerSprite.setFrameSequence(playerSprite.MOVEMENT_SEQUENCE);
}
}
protected void keyReleased(int keyCode){
super.keyReleased(keyCode);
regGameCodes=0;
keyPress=0;
playerSprite.setFrameSequence(null);
playerSprite.setFrame(0);
}
public void commandAction(Command c, Displayable d) {
if (c == backCommand) {
midlet.mainMenuShow();
}
}
public void newGame(){
try{
theLevels.setMap(1);
} catch (Exception e){
System.out.println(e);
}
display.setCurrent(this);
repaint();
}
private void drawScreen(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(cityBackground, 0, -20, Graphics.TOP | Graphics.LEFT);
g.setColor(0x0000ff);
background = theLevels.getBackground();
layerManager.setViewWindow(scnX*MOVE_RATE,30,176,190);
layerManager.paint(g,0,0);
flushGraphics();
}
}
my player sprite class (update 3/29/06)
package de.enough.polish.example;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
public class PlayerSprite extends Sprite{
static final int MOVE = 5;
static final int XMOVE = 5;
static final int DMOVE = MOVE*100/141;
private static final int END_BUFFER = 22;
private Image image;
private int xPos;
private int yPos;
private int frameWidth;
private int frameHeight;
private int scnWidth, scnHeight;
private int frame;
public static final int[] MOVEMENT_SEQUENCE={1, 2, 3, 4};
private boolean isRun;
private int currentFrame = 0;
private int maxSpriteFrames = 5;
private int firstWalkingFrame;
private int standingFrame = 0;
private TheLevels theLevels;
public PlayerSprite(Image image, int frameWidth, int frameHeight, int scnWidth, int scnHeight) throws Exception {
super(image, frameWidth, frameHeight);
defineReferencePixel(frameWidth/2, frameHeight/2);
this.scnWidth = scnWidth;
this.scnHeight = scnHeight;
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.isRun = false;
this.setFrame(0);
}
public void startPosition() {
setPosition(scnWidth/2,scnHeight-85);
}
public void moveLeft() {
if (getX() - XMOVE > 0) {
move(XMOVE * -1,0);
setTransform(TRANS_MIRROR);
nextFrame();
isRun = true;
System.out.println("Left");
System.out.println("X: "+ MOVE);
}
}
public void moveRight() {
if (getX() + XMOVE + END_BUFFER < (scnWidth*6)+96) {
move(XMOVE,0);
setTransform(TRANS_NONE);
nextFrame();
isRun = true;
System.out.println("Right");
System.out.println("Y: "+ MOVE);
}
}
public void moveUp() {
if (getY() - MOVE > scnHeight-90)
move(0, MOVE * -1);
nextFrame();
isRun = true;
System.out.println("Up");
}
public void moveDown() {
if (getY() + MOVE + getHeight() < scnHeight+20)
move(0,MOVE);
nextFrame();
isRun = true;
System.out.println("Down");
}
public void moveUpLeft(){
if(xPos-DMOVE>0&&yPos-DMOVE>0)
move(-DMOVE,-DMOVE);
setTransform(TRANS_MIRROR);
nextFrame();
isRun = true;
}
public void moveUpRight(){
if(xPos+DMOVE+frameWidth<scnWidth&&yPos-DMOVE>0)
move(DMOVE,-DMOVE);
setTransform(TRANS_NONE);
nextFrame();
isRun = true;
}
public void moveDownLeft(){
if(xPos-DMOVE>0&&yPos+DMOVE+frameHeight<scnHeight)
move(-DMOVE, DMOVE);
setTransform(TRANS_MIRROR);
nextFrame();
isRun = true;
}
public void moveDownRight(){
if(xPos+DMOVE+frameWidth<scnWidth&&yPos+DMOVE+frameHeight<scnHeight)
move(DMOVE,DMOVE);
setTransform(TRANS_NONE);
nextFrame();
isRun = true;
}
public int giveX(){
return xPos;
}
public int giveY(){
return yPos;
}
public void setRunOff(){
if (isRun){
isRun = false;
setFrameSequence(null);
setFrame(0);
}
}
}
[Edited by - thelegendofzaku on March 29, 2006 11:05:34 PM]
|