Need help with making character jump in small game.

edited October 2016 in Questions about Code

So I am making a very simple and small game for class and I have the movements down so that this character (block) can move left and right, however I am pretty stumped when it comes to making the character jump. I have been up for hours trying to figure this out and I have not found a solution. Any tips? Here is my code:

float xpos;
float ypos;

void setup(){
 size(640,480);
 xpos = 50;
 ypos = 400;
}

void draw(){
background(255);
  playerDraw();
 playerMove();
}

void playerDraw(){
  fill(0,255,255);
  rect(xpos,ypos, 20,20);
}

void playerMove(){

  float speed =2.0 ;
  if(holdLeft){
   xpos -= speed;
 }
 if(holdRight){
  xpos += speed; 
 }
if(keyCode == UP){
if(ypos > 400){
 ypos -=20;
}
else{ypos+=20;}
}
}

//this next block of code is held in a different tab labeled keyboard

boolean holdLeft = false,
        holdRight = false;



void setSignal (boolean setTo) {
 if(keyCode == LEFT){
   holdLeft = setTo;
    }
    if(keyCode == RIGHT){
   holdRight = setTo;
 }

}

void keyPressed(){
  setSignal(true);


  }


void keyReleased(){
  setSignal(false);


}
Tagged:

Answers

  • edited October 2016 Answer ✓

    Hi, you need to format your code. To do that, edit post, select code and hit ctrl+o. Ensure there is an empty line above and below your code.

    Regarding your question, here I implemented a way to do it:

    float xpos; 
    float ypos;
    
    void setup() { 
      size(640, 480); 
      xpos = 50; 
      ypos = 400;
      frameRate(25);
    }
    
    void draw() { 
      background(255); 
      playerDraw(); 
      playerMove();
    }
    
    void playerDraw() { 
      fill(0, 255, 255); 
      rect(xpos, ypos, 20, 20);
    }
    
    void playerMove() {
    
      float speed =2.0 ; 
      if (holdLeft) { 
        xpos -= speed;
      } 
      if (holdRight) { 
        xpos += speed;
      } 
    
      if (ypos>=400)
        ypos=400;
      else
        ypos=ypos+5;
    }
    
    //this next block of code is held in a different tab labeled keyboard
    
    boolean holdLeft = false, holdRight = false;
    
    void setSignal (boolean setTo) { 
      if (keyCode == LEFT) { 
        holdLeft = setTo;
      } 
      if (keyCode == RIGHT) { 
        holdRight = setTo;
      }
    }
    
    void keyPressed() { 
      setSignal(true);
    }
    
    void keyReleased() { 
      setSignal(false);
    
        if (keyCode == UP) { 
        ypos=ypos-15;
        //if (ypos > 400) { 
        //  ypos -=20;
        //} else {
        //  ypos+=20;
        //}
      }
    }
    

    To implement jumping, you need to think about reality. In physics, gravity is what pulls you down at all times. I implemented that effect in one of your functions that you call in draw(). Then to introduce the jump, I managed it directly in keyPressed(). Notice that if you implement the jump as you did in your original code, keyCode is kept in UP state until another key is pressed. Hence, when you run your code version, the key UP state is called several times. You can see this >>undesirable<< effect if you slow down your sketch using frameRate() function in setup.

    I hope this helps,

    Kf

  • Thank you! so the last commented code you put under the keyReleased is the code I originally had there? And the way you did the jump function was interesting, making the jump happen when the key is released i didn't think of that.

  • also @kfrajer im trying to figure out a way to get the character to not jump more than once because you can jump as much as youd like, i just want one jump and them you cant jump after you hit the ground. Any tips? Im still looking into it.

  • Answer ✓

    Use a boolean variable - a flag. When you jump, you enable the flag. When you return back to ground, you disable the flag. Only jump when the flag is disable. Of course the initial condition of your flag is false.

    Another way to approach it is instead of using

    ypos=ypos-15;

    In line 59, use ypos = 400-15; If you decide to go this way, you only need to change this line.

    Kf

Sign In or Register to comment.