Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to move my background left if the user hits the right key. and right if the user hits the left key, so that it will look like the player is moving.

The problem is that whenever I hold the right key, there is a pause of 1 second, then the background moves to the left. I want to make it smoother so that if the user holds the right key, the background moves left immediately.

Note that the background is a 2d int array with just 2's in it, and in Paint I am drawing a green rectangle, so the background is green.

main.java

.....
public void keyPressed(KeyEvent e){
   int keys = e.getKeyCode();

    if(keys == KeyEvent.VK_RIGHT)
    {
        level_class.hitRIGHT();  //move camera left
    }
    else if(keys == KeyEvent.VK_LEFT)
    {
        level_class.hitLEFT();  //move camera right
    }
    }/*** end of key pressed method ***/


    /*** KEY RELEASED ***/
    public void keyReleased(KeyEvent e)
    {
        int keys = e.getKeyCode();

            if(keys == KeyEvent.VK_RIGHT)
            {

            }
            else if(keys == KeyEvent.VK_LEFT)
            {

            }
    }/*** end of key pressed method ***/

Level class:

public class levels {
//note my array is alot bigger than this. i just made a small one here
    private int map01[][] = { { 2, 2, 2 },
                            { 2, 2, 2} };

    int camera_pos_x = 0;       
    int camera_pos_y = 0;   
    int camera_speed = 5;   //camera speed

    .....
    /*** move camera left ***/
public void hitRIGHT()
{
    camera_pos_x += camera_speed;
}
public void hitLEFT()
{
    camera_pos_x -= camera_speed;
}

    public void paint(Graphics g) {
        for (int y = 0; y < map01.length; y++) // rows
        {
            for (int x = 0; x < map01[y].length; x++) // cols
            {
                if (map01[y][x] == 0)
                {
                    g.setColor(Color.green);
                    g.fillRect(x * tile_size - camera_pos_x, y * tile_size -camera_pos_y, tile_size, tile_size);
                }
            }
        }// end of main for loop
    }// end of paint method
}
share|improve this question
    
What api is this? Using int for location/offsets will have less accuracy than a float. And if you have non-constant timesteps that will cause stutters. –  monkey Mar 24 '13 at 5:16
    
i c. how about the half a sec wait. any idea?. –  dave Mar 24 '13 at 6:50
    
Woah, are you in any control of how long it waits before moving? You say it takes a second before it moves, is that what your issue is, or is that something you're in control of? Half a second wait would be 2 FPS, which will not be sufficient for the illusion of motion/animation. Movies usually run at 24 FPS (I think some animations run at less to cut costs), but for a video game I don't find this is enough. You should aim for 30 FPS (30 times a second) or even better 60 FPS. –  Jeff Mar 24 '13 at 14:57
add comment

2 Answers

Yes, as ScrambledRK said, you should be settting flags. You should create 2 boolean variables:

Boolean left_key_down = false;
Boolean right_key_down = false;

and set them to appropriate values in the event listeners:

public void keyPressed(KeyEvent e) {
   int keys = e.getKeyCode();
   if(keys == KeyEvent.VK_RIGHT)
   {
      right_key_down = true;
   }
   else if(keys == KeyEvent.VK_LEFT)
   {
      left_key_down = true;
   }
}
public void keyReleased(KeyEvent e)
{
   int keys = e.getKeyCode();
   if(keys == KeyEvent.VK_RIGHT)
   {
      right_key_down = false;
   }
   else if(keys == KeyEvent.VK_LEFT)
   {
      left_key_down = false;
   }
}

Then, do your motion in your main update loop based on those flags.

// main loop blah blah
if( left_key_down ) hitRIGHT();
if( right_key_down ) hitLEFT();

That should result in smooth motion.

share|improve this answer
add comment

you are prob. better of checking the key-pressed status every frame and using the key-listener to just flag the key as pressed (using a map/array with the keycode as key).

I'm not familiar with java, but flash has the same issue. The reason might be related to the way a frame is split up internally and the fps your app runs in similar to the AS2/AS3 VM

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.