We are about to switch to a new forum software. Until then we have removed the registration on this forum.
OK, I'm creating a side scrolling game and made it where the player runs to the side, but can't run backwards. He gotta jump from platform to platform, but when I hit jump it just goes up without going sideways. Here the player code;
class player{
int x,y,r, dir;
float grav, jump, spd;
boolean land;
player(int x, int y){
this.x = x;
this.y = y;
r = 10;
}
void show(){
fill(0,255,0);
ellipse(x,y,r*2,r*2);
}
void move(){
if (jump > 0){
jump -= 1;
}
for (int i = 0; i < Platform.length; i++){
if (y+r > Platform[i].y - 5 && x > Platform[i].x && x < Platform[i].x + Platform[i].w && y < Platform[i].y){
grav = 0;
land = true;
break;
} else {
grav = 1;
land = false;
}
}
spd = grav - jump;
y += spd;
x += dir * 5;
dir = 0;
if (y > height && room == 1){
if (score > highScore[0]){
highScore[4] = highScore[3];
highScore[3] = highScore[2];
highScore[2] = highScore[1];
highScore[1] = highScore[0];
highScore[0] = score;
}
else if (score > highScore[1]){
highScore[4] = highScore[3];
highScore[3] = highScore[2];
highScore[2] = highScore[1];
highScore[1] = score;
}
else if (score > highScore[2]){
highScore[4] = highScore[3];
highScore[3] = highScore[2];
highScore[2] = score;
}
else if (score > highScore[3]){
highScore[4] = highScore[3];
highScore[3] = score;
}
else if (score > highScore[4]){
highScore[4] = score;
}
room = 2;
frameRate(60);
}
}// move
}
can someone tell me how to get him to use x and y at same time;
here the keyPressed
if (key == ' ' && Player.land == true){
Player.jump = 15;
}
Answers
Why do you set dir = 0?
Then x won’t change?
after the player moves I need him to stop moving till you press the button again or he will keep on going
Yes but when he jumps you want to keep him going right?
yea, I do but when lands he needs to be able to stop and stay in one place.
So you need if-clause that checks if dir=0 is allowed
It’s not allowed during jump
So do if land = false do jump if land = true dir = 0?
This:
dir = 0;
must instead be
if land {
dir = 0;
}
ty i'll try it.
it still not working as I want here the x y code in player
spd = grav - jump; y += spd; x += dir * 5; if (land == true){ dir = 0; }
I want like a arc where player goes up then comes down a feet pixels away but mine is landing exactly where he started I know I tried him on the edge & also if press right when in air he zooms frontwards. I think I can correct that by checking if land = true in right click. here the jump code and fixed right pressed
if (key == ' ' && Player.land == true){ Player.jump = 15; Player.dir = 1; }
if (keyCode == RIGHT && Player.land == true){ Player.dir = 1; }
You could evaluate space bar and right key simultaneously
Or when he runs right, and jumps he continues to fly right
I tried adding an xspeed, but then the jump looks dumb, it goes more right before it goes up. It's not an archway like I was planning on.
if (key == ' ' && Player.land == true){ Player.jump = 15; Player.dir = 1; Player.xspeed = 45; }
if (keyCode == RIGHT && Player.land == true){ Player.dir = 1; Player.xspeed = 5; }
spd = grav - jump; y += spd; x += dir * xspeed;
In the moment of jumping yspeed is high negative and then gets smaller and then gets positive
yea it needs to be negative to lift off the ground.
Regarding trajectory: either increase negative yspeed or decrease xspeed when he jumps
TY, I'll try a few more things to get the jump I want right now he look dumb the way he jumps
could you post your entire code please?
typical jump code
I like your code but yours keeps going I would like mine to slow to a stop and only move if keypress jump left or right here the start up
`player Player; game g = new game(); menu m = new menu(); scoreboard SB = new scoreboard(); platform[] Platform = new platform[6]; int[] highScore = new int[5]; int score, room, s; boolean pause; void gameOver(){ Player = new player(100,200); frameRate(60); for (int i = 0; i < Platform.length; i++){ Platform[i] = new platform(50+(i*180),260,150); } score = 0; } void setup(){ size(700,300); gameOver(); }
void draw(){ background(0); noStroke(); textSize(12);
if (room == 0){ m.update(); } if (room == 1){ g.update(); } if (room == 2){ SB.update(); } if (pause == true){ textSize(32); text("PAUSED",350,150); } }
void keyPressed(){ if (key == ' ' && Player.land == true){ Player.jump = 15; } if (key == 'p'){ if (pause == false && room == 1){ pause = true; } else if (pause == true){ pause = false; } } if (key == '1' && room == 0 || key == '1' && room == 2){ gameOver(); room = 1; } if (key == '2' && room == 0){ room = 2; } if (key == '3' && room == 2 || key == '3' && room == 0){ exit(); } if (keyCode == RIGHT && Player.land == true){ Player.dir = 1; Player.xspeed = 5; } }`
here the player
class player{ float x,y,r,grav,jump,spd,dir,xspeed; boolean land; player(int x, int y){ this.x = x; this.y = y; this.r = 10; } void show(){ fill(255,0,0); ellipse(x,y,r*2,r*2); } void move(){ if (jump > 0){ jump -= 1; } for (int i = 0; i < Platform.length; i++){ if (x > Platform[i].x && y+r > Platform[i].y-3 && x < Platform[i].x + Platform[i].w && y < Platform[i].y){ grav = 0; land = true; break; } else { grav = 1; land = false; } } spd = grav - jump; y += spd; } }
here the platforms
class platform{ int x,y,w,h,a,spd; float space, hi, wi; platform(int x, int y, int w){ this.x = x; this.y = y; this.w = w; h = 10; spd = 1; } void show(){ fill(255,255,0); rect(x,y,w,h); } void move(){ x -= spd; // here we moving the platform but i wanna change this part so the player moves that why I need him to jump for (int i = 0; i < Platform.length; i++){ if (Platform[i].x + Platform[i].w < 0){ score += 1; s += 1; a = i-1; if (a == -1){a = 5;} space = random(30,50); hi = random(240,290); wi = random(3,5); wi = round(wi); Platform[i].x = Platform[a].x + Platform[a].w + (int)space; Platform[i].y = (int)hi; Platform[i].w = (int)wi*50; } } }// move }
I can't run your code
too much missing
maybe post an mcve
https://stackoverflow.com/help/mcve
here is a new version of MY code
going right only when you hold crs right
you can jump
stopping now when you release crs right during jump (but stopping only when the jump has ended)
TY very much. Your new code is helping and doing what I want
;-)