Results 1 to 5 of 5

Thread: keyPressed() only works with 'Cancel' key

  1. #1

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    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

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: keyPressed() only works with 'Cancel' key

    Where's the imports? Are the declared in the class you are extending? GameCanvas
    Code:
    import java.awt.KeyAdapter;
    or 
    import java.awt.KeyListener;

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: keyPressed() only works with 'Cancel' key

    Also keyPressed(KeyEvent e) takes a KeyEvent as an arg you have protected void keyPressed(int keyCode) which i don't ubderstand. Also should't you be checking for the KeyEvent that was fired off within the keyPressed() method? All i see is.
    Code:
    protected void keyPressed(int keyCode) {
     int gameAction = getGameAction(keyCode);
     if (gameAction==RIGHT) {
      staticX++;
     }
      staticX++;
    }

  4. #4

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    Re: keyPressed() only works with 'Cancel' key

    i am using the microedition and not the standard edition. I think what your saying is not j2me (correct me if im not right) coz i tried your imports and it didnt work (didnt compile). Im using the j2ME wireless toolkit to build and run my program. unfortunately, keypressed() method doesnt work. could someone point me what should i do about this? thanks.

    Thanks a lot Dilinger4 for the help.
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: keyPressed() only works with 'Cancel' key

    Hummmmmm. Im sure someone in the Java Mobility Forums could be of help. Did you try there at all? http://forum.java.sun.com/index.jspa

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width