×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    37

    a question about keyPressed and keyReleased

    hi

    I used this code to move an image over the display:
    import javax.microedition.lcdui.*;

    public class Displayable1 extends Canvas implements CommandListener {

    private Image image;
    private int x = 50;
    private int y = 50;
    private int speed = 5;
    Command exit;

    public Displayable1() {
    try {
    image = Image.createImage("/pressRelease/space1on.png");
    }
    catch (Exception e) {}

    exit = new Command("Exit", Command.EXIT, 1);
    addCommand(exit);
    setCommandListener(this);
    }
    protected void paint(Graphics g) {
    int anchor = Graphics.LEFT | Graphics.TOP;
    g.drawImage(image, x, y, anchor);
    }
    private void moveImage(int direction) {
    switch (direction) {
    case 1: x+=speed;
    break;
    case 2: y+=speed;
    break;
    case 3: x-=speed;
    break;
    case 4: y-=speed;
    break;
    }
    repaint();
    }
    public void commandAction(Command command, Displayable displayable) {
    }
    protected void keyPressed(int keyCode) {
    int action = getGameAction(keyCode);
    switch (action) {
    case Canvas.RIGHT:
    moveImage(1);
    break;
    case Canvas.LEFT:
    moveImage(3);
    break;
    case Canvas.UP:
    moveImage(4);
    break;
    case Canvas.DOWN:
    moveImage(2);
    break;
    }
    }
    }
    how can I add the keyReleased method?
    what I need is this: a key is pressed, the image moves and still moving until the key is released.
    so first keyPressed, then movement starts, and then when the keyReleased condition is true stops.
    can you help me with a code sample?

    thank you!
    eml

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    1

    hope this may help you

    I suggest you can put 4 boolean variables in your program, and in keyPressed method, a boolean variable set true when a corresponding key was pressed, and in keyReleased method, a boolean variable set false when a corresponding key was released. this may be little difficult, because you must put a thread in your program, it control image moving, when a boolean varible is true, image may move to corresponding direction.

    my english is poor, hope you can understand and this may help you.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1

    previous answer is correct...

    You should use thread anyway, not only for getting the keyRelease event, if you want a smooth animation.

    The key latching behavior that you want could be implemented using one int mask (using bitwise operations) instead of four booleans, but that's not that important.

    by the way, the behavior you need is part of the MIDP 2.0 spec
    It's in GameCanvas: suppressKeyEvents.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    37
    I hope that MIDP 2 will be supported by nokia phones soons.

    thanks for the answers

    eml

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    9
    Hi her is some code example width rectangle

    import javax.microedition.lcdui.*;

    public class Test5Canvas extends Canvas implements Runnable
    {
    static final int DELAY = 150;
    private Test5Midlet midlet;
    private volatile Thread animationThread = null;
    private int x,y;
    private int maxx,maxy;

    private boolean up,down,left,right=false;


    public Test5Canvas(Test5Midlet midlet)
    {
    this.midlet=midlet;
    maxx=getWidth();
    maxy=getHeight();
    x=maxx/2;
    y=maxy/2;
    }

    public void paint(Graphics g)
    {
    g.setColor(255,255,255);
    g.fillRect(0,0,maxx,maxy);
    g.setColor(255,0,0);
    g.fillRect(x,y,10,10);
    }


    public synchronized void start()
    {
    animationThread = new Thread(this);
    animationThread.start();
    }

    public synchronized void stop()
    {
    animationThread = null;
    }

    public void run()
    {
    Thread currentThread = Thread.currentThread();

    try
    {
    while (currentThread == animationThread)
    {
    long startTime = System.currentTimeMillis();
    KeyRepeated();
    repaint();
    serviceRepaints();
    long delay = System.currentTimeMillis() - startTime;
    if(delay < DELAY)
    {
    synchronized (this)
    {
    wait(DELAY-delay);
    }
    }
    else
    {
    currentThread.yield();
    }
    }
    }
    catch(Exception e)
    {
    }
    }

    public void keyPressed(int keyCode)
    {
    if(keyCode<0)
    midlet.exit();
    else
    {
    switch(keyCode)
    {
    case KEY_NUM2: up = true;break;
    case KEY_NUM8: down = true;break;
    case KEY_NUM4: left = true;break;
    case KEY_NUM6: right = true;break;
    }
    }
    }

    public void keyReleased(int keyCode)
    {
    switch(keyCode)
    {
    case KEY_NUM2: up = false;break;
    case KEY_NUM8: down = false;break;
    case KEY_NUM4: left = false;break;
    case KEY_NUM6: right = false;break;
    }
    }

    public void KeyRepeated()
    {
    if(up){if(y>1)y--;}
    if(down){if(y<maxy-15)y++;}
    if(left){if(x>1)x--;}
    if(right){if(x<maxx-15)x++;}
    }

    }

Posting Permissions

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