My player wont jump over the hole

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

    float gravity = 0.53;
    float friction = -0.9;
    
    Ball ball;
    
    void setup() {
      size(640, 360);
      ball = new Ball(11, height-20, 40);
      noStroke();
      fill(255, 204);
    }
    
    void draw() {
      background(0);
      text("Hit any key to jump", 322, 122);
      ball.move();
      ball.display();
    }
    
    // ---------------------------------------------------------------
    
    void keyPressed() {
      ball.jump();
    }
    
    // ================================================================
    
    class Ball {
    
      float x, y; // position 
    
      float diameter;
    
      float xadd = 1.4;
      float yadd = 0;
    
      Ball(float x_in, float y_in, 
        float diameter_in) {
    
        x = x_in;
        y = y_in;
    
        diameter = diameter_in;
      } 
    
      void move() {
    
        // gravitation ändert yadd
        yadd += gravity;
    
        // bewegung 
        x += xadd;
        y += yadd;
    
        if (y + diameter/2 > height) {
          y = height - diameter/2;
          yadd *= friction;
          if (yadd<.01)
            yadd=0.0;
        } else if (y - diameter/2 < 0) {
          y = diameter/2;
          yadd *= friction;
        }
      }
    
      void jump() {
        //inits a jump
        yadd = -12.3;  // high negative yadd
      }
    
      void display() {
        ellipse(x, y, diameter, diameter);
      }
    }//class
    //
    
  • edited June 2018

    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

  • Answer ✓

    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)

    float gravity = 0.53;
    float friction = -0.9;
    
    Ball ball;
    
    void setup() {
      size(1640, 360);
      ball = new Ball(11, height-20, 40);
      noStroke();
      fill(255, 204);
    }
    
    void draw() {
      background(0);
      text("Hit Space Bar to jump, csr right to move and r to reset ", 322, 122);
      ball.move();
      ball.display();
    
      if (keyPressed && keyCode == RIGHT) { 
        //ball.dir = 1; 
        ball.xadd = 1.4;
      } else {
        //
      }
    }
    
    // ---------------------------------------------------------------
    
    void keyPressed() {
    
      if (key == ' ' && ball.land) { 
        // ball.jump = 15;
        ball.jump();
      } 
      if (key == 'r') { 
        //reset
        ball.x=0;
      }
    }
    
    void keyReleased() {
      if (keyCode == RIGHT) {
        if (ball.land)
          ball.xadd = 0;
        else 
        ball.stopWish=true;
      }
    }
    
    // ================================================================
    
    class Ball { // ball 
    
      float x, y; // position 
    
      float diameter; // size 
    
      float xadd = 0;
      float yadd = 0;
    
      boolean land=true; // is not jumping
      boolean stopWish=false; 
    
      //constr
      Ball(float x_in, float y_in, 
        float diameter_in) {
    
        x = x_in;
        y = y_in;
    
        diameter = diameter_in;
      } //constr
    
      void move() {
    
        // gravity changes yadd
        yadd += gravity;
    
        // movement 
        x += xadd;
        y += yadd;
    
        if (y + diameter/2 >= height) {
          // stop jump
          land=true; 
          y = height - diameter/2;
          yadd *= friction;
          if (yadd<.01)
            yadd=0.0;
          if (stopWish) {
            xadd=0;
            stopWish=false;
          }//if
        } else if (y - diameter/2 < 0) {
          // upper screen border 
          y = diameter/2;
          yadd *= friction;
        }
      }
    
      void jump() {
        //inits a jump
        yadd = -12.3;  // high negative yadd
        land=false;
      }
    
      void display() {
        ellipse(x, y, diameter, diameter);
      }
      //
    }//class
    //
    
  • TY very much. Your new code is helping and doing what I want

Sign In or Register to comment.