i had a question but i am not sure what the problem is.
if user keep right key down. than camera should move left.and my player should fall down bc there are two empty(sky,'0') under player.

move camera.

2d map: (0=sky,1=ground,2=player)


int camera_pos_x = 0;
int camera_pos_y = 0;
int camera_speed = 4; 
int tile_size = 32;  //width and height same
if(left) //if user press left key
    camera_pos_x += camera_speed;
    camera_pos_x -= camera_speed;

now to move level iam doing doing this in paint method in level class.

for (...) {
     for (...) {
         if (map01[y][x] == 1) {
            g.fillRect(x * tile_size - camera_pos_x, y * tile_size - camera_pos_y, tile_size, tile_size); 

above code will move the bottom floor when user moves left and right.

than i am setting a collision box around every where i see '1'(ground)

for(int y = 0; y < map01.length; y++){
     for(int x = 0; x < map01[y].length; x++){
         if(map01[y][x] == 1){
            rect.add(new Rectangle(x*tile_size-camera_pos_x, y*tile_size, tile_size, tile_size));

not to check for collision i am doing:

for(int i = 0; i < rect.size(); i++){
        on_ground = p.getY() ; 

if i print the this.rect.get(i) in if statment: i get

now if i move to right block: output is

should that be?

also my player jump method:

int x,dx,width,height;
double dy = 6.0;  //chage in y over time
double gravity = 0.3; //0.2 pull down when jump
int fall_speed = 4; 

boolean fall = true;  //start the game player falling
boolean jump = false; //start the game player not jumping

if(up && !fall && !jump)//player has to be on ground
            jump = true;
            fall = false;
            dy = 6.0;      //reset
    if(jump)  //safe to jump. player is on ground and not falling.
        dy -= gravity;     //move player up
        y -= dy;       

            if(y + height == og) //if on ground. so stop the jump
            jump = false; 
            fall = false;
    else if(!jump)                //player is falling 
            fall = true;
            y += fall_speed;      //player falling speed

We are just as confused as you are!

  • Why does the right key move the camera left? ("... if user keep right key down. than camera should move left ...")
  • Why would moving the camera make the player fall down? ("... camera should move left.and my player should fall down ...")
  • If the tile size is 32 (line 9), where does the "5" come from in the Rectanges that you print?
This article has been dead for over six months. Start a new discussion instead.