les say i have a map:

//my map is biiger but this is just a ex.

``````int map[][] = {{1, 1, 1 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
int camera_pos_x = 0;
int camera_pos_y = 0;
int camera_speed = 4;
int tile_size = 32;  //width and height same
``````

now to move the camera i can do this:

``````if(left)
{
camera_pos_x += camera_speed;
}
if(right)
{
camera_pos_x -= camera_speed;
}
``````

than i can print the map:

``````for(...)
{
for(...)
{
if (map[y][x] == 1)
{
g.setColor(Color.green);
g.fillRect(x * tile_size - camera_pos_x,
y * tile_size - camera_pos_y, tile_size, tile_size);
}
}
}
``````

1st question is that is how we get the mapwidth and map height? i am not sure if i need to do * tile_size.

``````private int map_width = map.length * tile_size;
private int map_height = map[0].length * tile_size;
``````

camera collsion
if user hold down left key map keep on moving right. and its moves too much and u can see while background at bottom. and the way i fix this problem was:

``````if(camera_pos_x < 0)
{
camera_pos_x = 0;
}
``````

this way camera cant go lower than 0.

2nd question is that how can i fix the same problem other way?
i tried this but didnt work:

``````if(camera_pos_x > map_width - m.getWINDOW_WIDTH())
{
//camera_pos_x = map_width - m.getWINDOW_WIDTH();
}
``````

i dont what wrong with my logic.

1
Contributor
1
19
Views
4 Years
Discussion Span
Last Post by game06

got it

``````private int map_width = map.length * tile_size;
private int map_height = map[0].length * tile_size;
``````

where wrong, its

``````private int map_height = map.length * tile_size;
private int map_width = map[0].length * tile_size;
``````