×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Sep 2003
    Location
    Sweden, Goteborg
    Posts
    3

    Smooth movment of a marker

    Hallo,

    I am doing my first game for cellur phones(Nokia 6610). My program will show a background in FullCanvas. Then I randomly draw a target which moves along the the screen from the bottom/center of the screen to the randomly selected end point where it disapears. The player will follow the target with his sight by using the arrow buttons or 2,4,6,8 buttons. He fires with button number 5.

    Everything works very good except for the sight. It does move but not as smooth that I want. The speed of the sight when I press the buttons every time I want the sight to move is 5 pixels. When I press the sight move button down (keyRepeated(...))the speed is 10. The sight jumps a away... I think it takes some time to repaint the screen

    My question is... is it possible to make the sight move smooth on the screen and faster then the object??? Like the mouse pointer over a computer screen...

    I send a little bit of my source code if it is to any help

    synchronized void start(){
    running = true;
    animationThread = new Thread(this);
    animationThread.start();
    }
    public void run()
    {
    try{
    while(running){
    repaint(0,0,canvasWidth, canvasHeight);
    //serviceRepaints();
    Thread.sleep(100);
    }
    }catch(Exception e){}
    }
    synchronized void stop(){running = false;}
    public void paint(Graphics g){ drawAnimation(g);}
    private void drawAnimation(Graphics gr) {
    Graphics g = gr;
    if(this.isDoubleBuffered()){
    g = graphics;
    }
    try{
    background.draw(g);
    gunSight.draw(g);
    if(target.isTargetAlive()){
    target.draw(g);
    target.Position();
    }else{
    target.newPosition(random.nextInt() & 0x7f);
    }
    gr.drawImage(buffer,0,0,g.LEFT|g.TOP);
    }catch(Exception e){}
    }
    protected void keyPressed(int keyCode){
    if(keyCode == KEY_SOFTKEY2){
    midlet.exitRequested();
    }else{
    int action = getGameAction(keyCode);
    switch(action){
    case Canvas.RIGHT:
    gunSight.moveSight(1,5);
    break;
    case Canvas.LEFT:
    gunSight.moveSight(2,5);
    break;
    case Canvas.UP:
    gunSight.moveSight(3,5);
    break;
    case Canvas.DOWN:
    gunSight.moveSight(4,5);
    break;
    case Canvas.FIRE:
    break;
    }
    }
    }
    protected void keyRepeated(int keyCode){
    int action = getGameAction(keyCode);
    switch(action){
    case Canvas.RIGHT:
    gunSight.moveSight(1,10);
    break;
    case Canvas.LEFT:
    gunSight.moveSight(2,10);
    break;
    case Canvas.UP:
    gunSight.moveSight(3,10);
    break;
    case Canvas.DOWN:
    gunSight.moveSight(4,10); break;
    }
    }
    }

    David Kihlström

  2. #2
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    134
    Did you test how much times a second the system calls keyRepeated()?

    Say that the system calls keyRepeated() a hundred times a second, in that case your markers speed would be 100 * 10pixels = 1000 pixels a second and your marker drifts off.....

    To solve it I quess you should implement a state machine in a separate game loop thread or in your run() method.

    The statemachine should handle the events you now handle in the keyPressed() and keyRepeated() methods. The keyPressed() and keyRepeated() methods should merely set the state:

    public class bla {
    private boolean key_right_pressed = false;
    private boolean key_right_released = false;
    private boolean key_right_repeated= false;

    protected void keyPressed(int keyCode){
    int action = getGameAction(keyCode);
    switch(action){
    case Canvas.RIGHT:
    key_right_pressed=true;
    break;
    }

    protected void keyRepeated(int keyCode){
    int action = getGameAction(keyCode);
    switch(action){
    case Canvas.RIGHT:
    key_right_repeated=true;
    break;
    }

    protected void keyReleased(int keyCode){
    int action = getGameAction(keyCode);
    switch(action){
    case Canvas.RIGHT:
    key_right_released=true;
    break;
    }

    public void run()
    {
    try{
    while(running){
    // service buttons
    if( key_right_repeated ) {
    // move fast
    gunSight.moveSight(1,10);
    } else
    if( key_right_pressed ) {
    // move slow
    gunSight.moveSight(1,5);
    }
    if( key_right_released ) {
    // reset button state
    key_right_repeated=false;
    key_right_pressed=false;
    key_right_released=false;
    }

    repaint(0,0,canvasWidth, canvasHeight);

    // This sleep must be removed since it doesn't result in a game loop of 10 frames a second, but a gameloop with at most 10 frames a second...
    Thread.sleep(100); //
    }
    }catch(Exception e){}
    }


    NOTE: By implementing it this way, as long as you keep your button pressed, the event will run.... ie there actually is no need for the keyRepeated() method...

    gr,

    Danny

Posting Permissions

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