Newbie way for standing and jumping on platforms

Hello,

Java beginner here, trying to make my first game. I searched around on the forum and found that the most common way for standing on platforms is using some kind of 2D tiles, I was wondering if there is an easier way because this is still a bit complex for me? Or if anyone knows some beginner friendly tutorials for this.

This is what I have currently: https://www.dropbox.com/s/5xky5t4o4s7mq6y/game.jar

My goal is to make more platforms where i’m able to jump onto, but the problem i’m having is that it’s gonna be impossible to write code for this the way i’m doing it. Currently I have this for the second platform

public void run() {
		x = 100;
		y = 100;
		while(true){
			
			
			if(x >= 630 && y >= 330 && y <= 400){
				
				y = 330;
			}
			
			if(y <= 460 && jump != true && x <= 630){
				y+= 10;
			}
			
			if(left == true){
				x -= 10;
			}
			
			if(right == true){
				x += 10;
			}
			
			if(down == true){
				y += 10;
			}
			
			if(y >= 460){ 
				y = 460;
			}
			changePositionPlayer(x, y); // 

as you can see this gonna be very exhausting to make for more platforms/levels?
Anyone has some tips or can point me in a good direction?

I think an easy way would be to have an array(or arraylist) of platform objects. Each of these platforms would have a position and size. When updating, loop through all your platforms and check collision with each of them. You could optimize and only check the platforms near the player, but I wouldn’t worry about this until it is a problem for you.

I ran into an issue JUST like this when I was creating my first game!
The solution I came up with was to have a template for platforms, I think the best way to show you how this would work is with example code.
While I was typing this Longarmx described exactly what im trying to show you!


/*In another file, named Platform.java*\
public class Platform {
   public double x;
   public double y;
   public Platform(double xToBeSet, double yToBeSet) {
      x = xToBeSet;
      y = yToBeSet;
   }
   public boolean pointIsCollidingWithThis(double xBeingTested, double yBeingTested) {
      if (xBeingTested >= x && x <= x + [width]) {
         if (yBeingTested >= y && y <= y + [height]) {
            return true;
         }
      }
      return false;
   }
}

/*Wherever you declare variables*/
int numberOfPlatforms = 0;
Platform[] platforms = new Platform[50];

/*Anywhere*\
public void createNewPlatform(double x, double y) {
   platforms[numberOfPlatforms] = new Platform(x,y);
   numberOfPlatforms ++;
}


/*In Game Loop*\
for (Platform p : platorms) {
   //Check if you can move down, if you cant dont allow movment.
}

I hope I helped all that took forever to type out. Good Luck!

What Longarmx said. The most basic idea (I think) would be to have an array with “solid” tiles and a function checkCollision. Press a key to move the player and then loop through the array and use your function player.checkCollision(tile) to see if the player will collide with anything. If this test returns true you cancel the movement and if it returns false you can move the player. This is by no means a great way to do it and you would need to fake gravity (so you don´t get stuck mid jump) but it is simple and can at least give you an idea how to think about it. :slight_smile:

There are a lot of good tutorials and explanations on the subject, a lot better than my “in the middle of the night and should´ve been sleeping 3 hours ago”-explanation. :slight_smile: I would suggest a search for “collision detection” and “AABB” (AABB = rectangle vs rectangle test) and take it from there.

Thanks guys, especially saucymeatman helped me a lot! There was a small typo mistake in your code just pointing it out for the people who wanna use it. I guess it should be (xBeingTested >= x && xBeingTested <= x + [width]) and if (yBeingTested >= y && yBeingTested <= y + [height]), after this it worked for me.

Really it worked?!

Wow! Untested things never work haha. Thanks for the medal and good luck with your game!