Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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 332 times)
0 Members and 1 Guest are viewing this topic.
Offline 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?
Offline 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.

Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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. Smiley

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

Offline 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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (46 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!