About movement

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

About movement

Postby LifeAsaCoder » Fri Jan 04, 2008 11:01 am

I really confuse about handle the movement. If i want to mve per block (per 10 pixel), somehow it read input 2 or 3 times (althought I press it and release really fast), so it moves 3 block instead of 1. Any suggestion to handle the movement? All I want is like pressing direction key in text box, first press make it move once, and if still pressed, it will moved rapidly in a customizable speed. Thanks
Here a sample code
src.zip
my source code
(1.56 KiB) Downloaded 312 times
LifeAsaCoder
 
Posts: 2
Joined: Thu Jan 03, 2008 5:23 pm

Re: About movement

Postby DevelopmentTeam » Sat Jan 05, 2008 4:39 am

The problem is that, keystatus is checked really fast like many times per second. And no doubt this will happen. So you have to check if the same key is kept pressed and avoid doing it until it is released or other keys are pressed. Here is the corrected code.


Code: Select all
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: Jan 4, 2008
* Time: 4:25:54 PM
* To change this template use File | Settings | File Templates.
*/
public class mainCanvas extends GameCanvas implements Runnable, CommandListener {
    private boolean isPlay;
    private int currentX;
    private int currentY;
    private int width;
    private int height;
    private int oldKey;
    private Graphics g;
    private Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private MID md;

    protected mainCanvas(MID midlet) {
        super(true);
        isPlay = false;
        width = getWidth();
        height = getHeight();
        g = getGraphics();
        addCommand(exitCommand);
        setCommandListener(this);
        md = midlet;
    }


    public void start() {
        Thread t = new Thread(this);
        isPlay = true;
        t.start();
    }

    private void input() {
        int keyStates = getKeyStates();

        if(oldKey != keyStates) {
// Left
        if ((keyStates & LEFT_PRESSED) != 0)
            currentX = Math.max(0, currentX - 1);
// Right
        if ((keyStates & RIGHT_PRESSED) != 0)
            if (currentX + 5 < width)
                currentX = Math.min(width, currentX + 1);
// Up
        if ((keyStates & UP_PRESSED) != 0)
            currentY = Math.max(0, currentY - 1);
// Down
        if ((keyStates & DOWN_PRESSED) != 0)
            if (currentY + 10 < height)
                currentY = Math.min(height, currentY + 1);

      oldKey = keyStates;
        }
    }

    public void run() {

        while (isPlay) {
            reDraw();
            input();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    public void reDraw() {
        g.setColor(0xFFFFFF);
        g.fillRect(0, 0, width, height);

        int x = currentX * 10;
        int y = currentY * 10;
        g.setColor(0x000000);
        g.fillRect(x, y,10, 10);

        flushGraphics();
    }

    public void commandAction(Command c, Displayable displayable) {
        if (c == exitCommand) {
            md.exitProgram();
        }
    }
}
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: About movement

Postby LifeAsaCoder » Sat Jan 05, 2008 11:49 am

True. It's solved part my the problem. The another problem is, on my code, it read input while it redraw the graphic, it share the same thread. so while the key is pressed 2 times and it still not refreshing the screen(because input is read right before redraw) it only read 1 movement. I wonder if i press the button repidly, the block move rapidly too, despite the update rate of screen. I'm still new to J2ME (3 month) so I don't know the right structure to make game movement. Thanks.
LifeAsaCoder
 
Posts: 2
Joined: Thu Jan 03, 2008 5:23 pm

Re: About movement

Postby DevelopmentTeam » Mon Jan 07, 2008 7:39 am

Code: Select all
    import javax.microedition.lcdui.game.GameCanvas;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Displayable;

    public class mainCanvas extends GameCanvas implements Runnable, CommandListener {
        private boolean isPlay;
        private int currentX;
        private int currentY;
        private int width;
        private int height;
        private int oldKey;
        private Command exitCommand = new Command("Exit", Command.EXIT, 1);
        private MID md;

        protected mainCanvas(MID midlet) {
            super(true);
            isPlay = false;
            width = getWidth();
            height = getHeight();
            addCommand(exitCommand);
            setCommandListener(this);
            md = midlet;
        }

        public void start() {
            Thread t = new Thread(this);
            isPlay = true;
            t.start();
        }

        private void input() {
            int keyStates = getKeyStates();

            if(oldKey != keyStates) {
    // Left
            if ((keyStates & LEFT_PRESSED) != 0)
                currentX = Math.max(0, currentX - 1);
    // Right
            if ((keyStates & RIGHT_PRESSED) != 0)
                if (currentX + 5 < width)
                    currentX = Math.min(width, currentX + 1);
    // Up
            if ((keyStates & UP_PRESSED) != 0)
                currentY = Math.max(0, currentY - 1);
    // Down
            if ((keyStates & DOWN_PRESSED) != 0)
                if (currentY + 10 < height)
                    currentY = Math.min(height, currentY + 1);

          oldKey = keyStates;
            }
        }

        public void run() {
            while (isPlay) {
                repaint();
                serviceRepaints();
                input();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }

        protected void paint(Graphics g) {
            g.setColor(0xFFFFFF);
            g.fillRect(0, 0, width, height);

            int x = currentX * 10;
            int y = currentY * 10;
            g.setColor(0x000000);
            g.fillRect(x, y,10, 10);
        }

        public void commandAction(Command c, Displayable displayable) {
            if (c == exitCommand) {
                md.exitProgram();
            }
        }
    }
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 1 guest

cron