Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: 2d game java movement

  1. #1
    Join Date
    Mar 2013
    Thanked 0 Times in 0 Posts

    Default 2d game java movement

    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)
    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() - 4; //- speed i dont know why

    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

  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,643 Times in 2,613 Posts

    Default Re: 2d game java movement

    Please post a small complete program that compiles, executes and shows the problem.

Similar Threads

  1. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  2. WASD Movement! Need Help!
    By Alex555 in forum Java Theory & Questions
    Replies: 12
    Last Post: April 19th, 2012, 01:59 PM
  3. How to control gain between mouse movement and cursor movement ?
    By DrPete in forum Java Theory & Questions
    Replies: 3
    Last Post: March 12th, 2012, 08:27 AM
  4. 3D camera movement
    By macko in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2011, 08:53 AM
  5. movement
    By mlan in forum Java Theory & Questions
    Replies: 4
    Last Post: February 15th, 2010, 10:57 PM