Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (767) Games in Android Showcase (230) games submitted by our members Games in WIP (854) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Newbie way for standing and jumping on platforms  (Read 1528 times) 0 Members and 1 Guest are viewing this topic.
mycu

Junior Newbie

 « Posted 2013-12-05 00:26:27 »

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
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31 `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?
Longarmx
 « Reply #1 - Posted 2013-12-05 00:55:37 »

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.

saucymeatman
 « Reply #2 - Posted 2013-12-05 01:06:53 »

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!
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33 `/*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!
 Games published by our own members! Check 'em out!
Axeman

Senior Devvie

Medals: 7

 « Reply #3 - Posted 2013-12-05 01:27:17 »

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.

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. I would suggest a search for "collision detection" and "AABB" (AABB = rectangle vs rectangle test) and take it from there.
mycu

Junior Newbie

 « Reply #4 - Posted 2013-12-05 13:27:49 »

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.

saucymeatman
 « Reply #5 - Posted 2013-12-05 20:51:14 »

Really it worked?!

Wow! Untested things never work haha. Thanks for the medal and good luck with your game!
Pages: [1]
 ignore  |  Print

 EgonOlsen (1328 views) 2018-06-10 19:43:48 EgonOlsen (1205 views) 2018-06-10 19:43:44 EgonOlsen (929 views) 2018-06-10 19:43:20 DesertCoockie (1330 views) 2018-05-13 18:23:11 nelsongames (1110 views) 2018-04-24 18:15:36 nelsongames (1359 views) 2018-04-24 18:14:32 ivj94 (2093 views) 2018-03-24 14:47:39 ivj94 (1251 views) 2018-03-24 14:46:31 ivj94 (2249 views) 2018-03-24 14:43:53 Solater (803 views) 2018-03-17 05:04:08
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org