2
\$\begingroup\$

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
}
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – monkey
    Mar 24, 2013 at 5:16
  • \$\begingroup\$ i c. how about the half a sec wait. any idea?. \$\endgroup\$
    – dave
    Mar 24, 2013 at 6:50
  • \$\begingroup\$ 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. \$\endgroup\$
    – Jeff
    Mar 24, 2013 at 14:57

2 Answers 2

2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .