We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I want that my ball jump from platform A to platform B. If do not touch the platform, falls.
boolean salta = false;
boolean baixa = false;
boolean esquerra = false;
boolean dreta = false;
boolean stop=false;
//PImage img;
float cercleX = 50;
float cercleY = 50;
float gravetat = 0.5;
float posx = 0;
float posy = 50;
float vy = -1;
float bounce = -0.5;
//color c = get(0,0);
//////////////////////////////////////////////////////////////////////////////////////
void setup() {
size (1280, 720);
smooth();
noStroke();
//img = loadImage("background1-720.png");
}
void draw() {
background (255);
//image(img, 0, 0);
//posx++;
frameRate (70);
fill(0);
rect(0, 400, 500, 100);
rect(600, 400, 500, 100);
fill(255, 0, 255);
ellipse(posx, posy, 20, 20);
if (!stop) {
vy += gravetat;
posy += vy;
}
if (posy > height - 330) {
vy = bounce * abs(vy);
if (abs(vy)<0.281) {
vy=0.0;
stop=true;
}
}
// booleans
if (salta) {
posy--;
stop=false;
vy=-5;
}
if (baixa) {
posy++;
}
if (esquerra) {
posx--;
}
if (dreta) {
posx++;
}
}
void keyPressed() {
if (keyCode == UP) {
salta = true;
} else if (keyCode == DOWN) {
baixa = true;
} else if (keyCode == LEFT) {
esquerra = true;
} else if (keyCode == RIGHT) {
dreta = true;
}
}
void keyReleased() {
if (keyCode == UP) {
salta = false;
} else if (keyCode == DOWN) {
baixa = false;
} else if (keyCode == LEFT) {
esquerra = false;
} else if (keyCode == RIGHT) {
dreta = false;
}
}
//
Answers
On line 50, you always make the ball bounce back up if its y position is 330 from the bottom edge of the sketch. ALWAYS. You do not ALWAYS want this to happen - sometimes it should keep falling.
Make the bouncing also depend on the ball's x position! HINT: What other conditions should you add to your conditional statement?
Maybe with the colour? But how can I say to the code that position is determined by colour?
Just use the X position. You know where the gap is, the start and the end of it, don't bounce if X is within the gap.
Okay, but imagine there are random platforms in y position and x position
Okay, but imagine you know the positions of those platforms too...
(Checking the colour is a valid solution, yes. How would you do that?)
I do not know! I just think it's a way of doing it. But honestly I have no idea how ...
Well, work out where the ball will be at the end of the current step and the use get() to find the colour at this new position...
Hey there again! I created a void for the ball & the platform. I don't know how to make ball falls when it's not on the platform.. can someone help me please?