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. but the problem is that player walks on top of empty blocks.(player doesnt move the level does)
move camera.
2d map: (0=sky,1=ground,2=player) 0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0 2,0,0,1,0,0,0,0,0 1,1,0,0,1,0,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 if(left) //if user press left key { camera_pos_x += camera_speed; } if(right) { camera_pos_x -= camera_speed; }
now to move level iam doing doing this in paint method in level class.
above code will move the bottom floor when user moves left and right.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);
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++){ if(p.getBounds().intersects(this.rect.get(i))){ System.out.println(this.rect.get(i)); on_ground = p.getY() - 4; //- speed i dont know why p.setY(on_ground); p.setJUMP(false); p.setFall(false);
if i print the this.rect.get(i) in if statment: i get
java.awt.Rectangle[x=0,y=20,width=5,height=5]
now if i move to right block: output is
java.awt.Rectangle[x=0,y=20,width=5,height=5]
should that be?
java.awt.Rectangle[x=5,y=20,width=5,height=5]
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 }