I need help with creating different platforms that my character is able to jump on and making jumping smoother

int PlatX = 400; //Platform X-coordinate
int PlatY = 300; //Platform Y-coordinate
PImage StonePlatform;
int yvel = 1;//Setting the speed of the platforms.
int blocklength = 100;//Setting the length of the platforms.
int blockheight = 10;//Setting the height of the platforms.
color c = color(0);//Setting the colour of the platforms.
int x = 470;
float CharacterX;
float Speed = 0;
float ystart = 534;
float jmp_speed = 10;
float Gravity = 0.55;
float CharacterY = ystart;
//(New Tab)
void setup() {
size(1000, 600);
CharacterY=60;
CharacterX=30;
}

void draw() {
background(150);
rect(0, 575, 600, 20);
rect(CharacterX, CharacterY, 20, 40);

if (CharacterY+Speed < ystart) {
CharacterY+=Speed;
Speed+=Gravity;
} else {
CharacterY = ystart;
Speed=3;
}
if (keyPressed) {
if ( key == ‘D’ || key == ‘d’) {
CharacterX=CharacterX+Speed;
}
}
if (keyPressed) {
if ( key == ‘A’ || key == ‘a’) {
CharacterX=CharacterX-Speed;
}
}
}

void keyPressed() {

if ( (keyCode == UP || key == ‘w’) && (CharacterY == ystart) )
{
Speed=-jmp_speed;
}
}
//(New tab)
Plat P = new Plat();
class Plat {
float Plat;
Plat() {
}
void Platf() {
rect(PlatX, PlatY, 20, 20) ;
//loadImage(“StonePlatform.png”);
}
}

Introduction

You want to have many platforms, so you want to have a list of platforms.
One Platform is described in the class Plat.
Since all platforms have a position, they have the same variables.
But each platform has different values in the variables.

  • Therefore platX and platY and many others (yvel, blocklength, blockheight and c) belong into the class and cannot be global variables.

see Objects / Processing.org

Details

You have a class Plat; platX and platY etc. belong
inside the class.

The List

Now have an ArrayList of Plat and

ArrayList<Plat> list = new ArrayList(); // before setup()

Using the List

In setup you say
list.add(new Plat()); // put one new Plat into the list

Either give them the position (and size!) in the constructor (list.add(new Plat( 170, 400 )); ) or just say
list.get(0).platX = 120; etc.

Display them in draw() :

for(Plat pl : list)
   pl.display ();

collision detection

Also, you want the collision detection in a for-loop like this.

for(Plat pl : list) {
   ..... pl ..... 
}
1 Like

I have been trying to use these instructions but I can’t seem to figure out just how to get it working

1 Like

you’ll eventually figure it out…

1 Like

Hello @Corn! To improve readability, kindly format your code by editing your post, selecting the code, and pressing the </> button. This will make it easier for us to assist you.

ezgif.com-gif-maker (1)