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
}
int
for location/offsets will have less accuracy than a float. And if you have non-constant timesteps that will cause stutters. \$\endgroup\$