|
-
Jul 20th, 2005, 02:31 AM
#1
Thread Starter
Addicted Member
keyPressed() only works with 'Cancel' key
I posted my entire code here, im sorry, I just want to know why the hell is my keyPressed() event only works when I press the 'cancel' button? Ive been searching this for hours, unfortunately i have no luck, so i decided to ask you guys. Am I missing something? I cant make the keyPressed() work.. Please help. Thanks
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class WatchaWordCanvas extends GameCanvas implements Runnable {
private boolean isPlay; // Game Loop runs when isPlay is true
private long delay; // To give thread consistency
private int currentX, currentY; // To hold current position of the 'X'
private int staticX, staticY;
private int bogposX, bogposY;
private int width; // To hold screen width
private int height; // To hold screen height
private int[] bogArray = new int[16];
private String[][] alphaMatrix = new String[4][4];
private int inputDelay;
// Sprites to be used
private Sprite playerSprite;
private Sprite backgroundSprite;
private Sprite bogCubesSprite1;
private Sprite[] bogCubesSprite = new Sprite[16];
private Sprite bogBackGround;
private Sprite bogTitleGround;
private Sprite bogWordBar, bogMenuBar;
private int dCtrA;
// Layer Manager
private LayerManager layerManager;
// Constructor and initialization
public WatchaWordCanvas() throws Exception {
super(true);
width = getWidth();
height = getHeight();
currentX = 0;
currentY = 0;
staticX = 2;
staticY = (height / 2) - 60;
delay = 20;
inputDelay = 0;
// Load Images to Sprites
Image playerImage = Image.createImage("/transparent.png");
playerSprite = new Sprite (playerImage,32,32);
Image bogCubesImage = Image.createImage("/watcha-cubesfilled.png");
bogCubesSprite1 = new Sprite(bogCubesImage,31,29);
bogCubesSprite1.setFrame(0);
Image bogBackImage = Image.createImage("/watcha-background2.png");
bogBackGround = new Sprite(bogBackImage);
Image bogTitleImage = Image.createImage("/watcha-title.png");
bogTitleGround = new Sprite(bogTitleImage);
Image bogWordImage = Image.createImage("/watcha-wordbar.png");
bogWordBar = new Sprite(bogWordImage);
Image bogMenuImage = Image.createImage("/watcha-menubar.png");
bogMenuBar = new Sprite(bogMenuImage);
for(dCtrA=0;dCtrA< bogCubesSprite.length;dCtrA++) {
bogCubesSprite[dCtrA] = new Sprite(bogCubesImage,31,29);
bogCubesSprite[dCtrA].setFrame(0);
}
layerManager = new LayerManager();
layerManager.append(bogTitleGround);
layerManager.append(bogWordBar);
layerManager.append(bogMenuBar);
for(dCtrA=0;dCtrA< bogCubesSprite.length;dCtrA++) {
layerManager.append(bogCubesSprite[dCtrA]);
}
layerManager.append(bogBackGround);
//Set the Positions of the Images
bogTitleGround.setPosition(0,0);
bogWordBar.setPosition(2,150);
bogMenuBar.setPosition(129,staticY - 2);
bogCubesSprite[0].setPosition(staticX, staticY);
bogCubesSprite[1].setPosition(staticX, staticY + 31);
bogCubesSprite[2].setPosition(staticX, staticY + 62);
bogCubesSprite[3].setPosition(staticX, staticY + 93);
bogCubesSprite[4].setPosition(staticX + 31, staticY);
bogCubesSprite[5].setPosition(staticX + 31, staticY + 31);
bogCubesSprite[6].setPosition(staticX + 31, staticY + 62);
bogCubesSprite[7].setPosition(staticX + 31, staticY + 93);
bogCubesSprite[8].setPosition(staticX + 62, staticY);
bogCubesSprite[9].setPosition(staticX + 62, staticY + 31);
bogCubesSprite[10].setPosition(staticX + 62, staticY + 62);
bogCubesSprite[11].setPosition(staticX + 62, staticY + 93);
bogCubesSprite[12].setPosition(staticX + 93, staticY);
bogCubesSprite[13].setPosition(staticX + 93, staticY + 31);
bogCubesSprite[14].setPosition(staticX + 93, staticY + 62);
bogCubesSprite[15].setPosition(staticX + 93, staticY + 93);
}
// Automatically start thread for game loop
public void start() {
isPlay = true;
Thread t = new Thread(this);
t.start();
}
public void stop() { isPlay = false; }
// Main Game Loop
public void run() {
Graphics g = getGraphics();
while (isPlay == true) {
//input();
drawScreen(g);
try { Thread.sleep(delay); }
catch (InterruptedException ie) {}
}
}
// Method to Handle User Inputs
private void input() {
int keyStates = getKeyStates();
//inputDelay = 0;
//Left
if ((keyStates & LEFT_PRESSED) != 0) {
if (inputDelay==0) {
inputDelay++;
staticX++;
}
if (inputDelay!=0) {
inputDelay++;
if (inputDelay==10) { inputDelay=0; }
}
}
// Right
if ((keyStates & RIGHT_PRESSED) !=0 )
if ( currentX + 5 < width) {
currentX = Math.min(width, currentX + 1);
playerSprite.setFrame(3);
}
// Up
if ((keyStates & UP_PRESSED) != 0) {
currentY = Math.max(0, currentY - 1);
playerSprite.setFrame(2);
}
// Down
if ((keyStates & DOWN_PRESSED) !=0)
if ( currentY + 10 < height) {
currentY = Math.min(height, currentY + 1);
playerSprite.setFrame(4);
}
}
protected void keyPressed(int keyCode) {
int gameAction = getGameAction(keyCode);
if (gameAction==RIGHT) {
staticX++;
}
staticX++;
}
// Method to Display Graphics
private void drawScreen(Graphics g) {
//g.setColor(0x00C000);
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
// updating player sprite position
//playerSprite.setPosition(currentX,currentY);
// display all layers
//layerManager.paint(g,0,0);
//layerManager.setViewWindow(55,20,140,140);
layerManager.paint(g,0,0);
bogCubesSprite[0].setPosition(staticX, staticY);
flushGraphics();
}
}
C++ Programming is overwhelming.
Dont let it overwhelm you or you'll fall into the oblivion of its perfection
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|