i trying to my background move left if user hit right key. and background right if user hit left key. that way it will look like my player is moving.
the problem is that when ever i hold right key, the background waits 1 sec than moves to left. i want to make it smoother so if user hold right key, than the background should move to left right way.
note the background is 2d int array with just '2''s in it. and in paint i am drawing a green rect. so 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 ***/
now in my level class i am setting variables
public class levels
{
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
}
...