Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-29-2012, 03:03 PM   PM User | #1
Digit348
New to the CF scene

 
Join Date: May 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Digit348 is an unknown quantity at this point
Need help adding simple gravity into a 2D sidescroller

Hello, digit348 here. Im currently working on remaking the original mario game for my APCS class and im stuck on trying to get the jump mechanics for him to work. Following is the current movement code i have:

Code:
public void keyPressed(KeyEvent e){
		int key = e.getKeyCode();
		int Max = 400;
		int Min = 10;
		
		if(x >= Max){
			dx = 0;
		}else if(x <=Min){
			dx =0;
		}else{
			if (key == KeyEvent.VK_UP){
				if(jumpCounter >0){
					dy = -2;
					jumpCounter = jumpCounter - 1;
					setJumping(true);
					}
			}else if (key == KeyEvent.VK_DOWN){
				dy = 2;
			}else if (key == KeyEvent.VK_LEFT){
				dx = -3;
				ImageIcon l = new ImageIcon("mImages/MarioSL2.gif");
				still = l.getImage();
			}else if (key == KeyEvent.VK_RIGHT){
				dx = 3;
				ImageIcon r = new ImageIcon("mImages/MarioSR2.gif");
				still = r.getImage();
			}else if(key == KeyEvent.VK_UP && key == KeyEvent.VK_RIGHT){
				dy = 2;
				dx = 3;
			}
		}
	}
	
	
	public void keyReleased(KeyEvent e){
		int key = e.getKeyCode();
		
		if(key == KeyEvent.VK_UP){
			dy = 0;
		}else if (key == KeyEvent.VK_DOWN){
			dy = 0;
		}else if (key == KeyEvent.VK_LEFT){
			dx = 0;
		}else if (key == KeyEvent.VK_RIGHT){
			dx = 0;
		}
		ImageIcon i = new ImageIcon("mImages/MarioSS.gif");
		still = i.getImage();
		if(x >= 400){
			x = 398;
		}else if(x <=10){
			x = 15;
		}
	}
The movement and jumping is handled through these:

Code:
public void move(){
		x = x+ dx;
	}
	public void jump(){
			y = y +dy;
	}
	public int getX(){
		return x;
	}
	
	public int getY(){
		return y;
	}
Finally i have the list of booleans i was attempting to use to control the jumping that i tried previously:

Code:
public void setJumping(boolean jumping){
	    this.jumping=jumping;
	    }
	  public boolean isJumping(){
	    return jumping;
	    }
	  
	public void setFalling(boolean falling){
		this.falling=falling;
		}
	public boolean isFalling(){
		  return falling;
		  }

	public void setOnGround(boolean onGround){
		  this.onGround=onGround;
		  }
	public boolean isOnGround(){
		  return onGround;
		  }
What i was attempting to do with the booleans was basiclly, if isFalling = true and isOnGround = false then he needs to be affected by gravity, however he needs to hit the max height be for this can happen.

Code:
public static int MAX_JUMP = 100;
Meaning he needs to be able to jump the 100 pixels, and then have gravity take effect on his sprite, causing him to return to the ground but not breaking the current collisions and sending him through it. (Done that more than a few times).

If anyother parts from my code would be helpful, please tell me and ill have them up shortly. Any and all help is greatly appreciated.
Digit348 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:47 AM.


Advertisement
Log in to turn off these ads.