This site requires JavaScript, please enable it in your browser!
Greenfoot back
AbdulrazaqAS
AbdulrazaqAS wrote ...

2022/1/9

How to jump properly?

AbdulrazaqAS AbdulrazaqAS

2022/1/9

#
I came across Stickman scenario with empty code so I am trying to write the code. I want the game to be the stickman jumping over enemies. I tried with this code block but it didn't work, the stickman will jump and come down within fractions of second.
import greenfoot.*;

/
 * This is a stick man. Make him run and jump.
 * 
 * @author 
 * @version 
 */
public class Stickman extends Actor
{
    private int velocity = 0;
    private int velCount = 0;
    /
     * Make the stickman act.
     */
    public void act() 
    {        
        run();
        checkKeyPress();
    }
    
    /
     * Method to check key press.
     */
    public void checkKeyPress(){
        int posX = getX();
        int posY = getY();
        if (Greenfoot.isKeyDown("up")){
            setLocation(posX, posY-70);
            setImage("jump.png");
            Greenfoot.delay(4);
            setImage("stand.png");
            setLocation(getX(), posY);
        }
    }
    
    /
     * Method to make stickman run.
     */
    public void run(){
        move(velocity+6);  //Starts running with 7
        int posY = getY();
        if (getX()+2>getWorld().getWidth()){
            //getWorld().addObject(new Stickman(), 0, posY);
            //getWorld().removeObject(this);
            setLocation(0, posY);
            velCount++;
            incVelocity();
        }
    }
    
    /**
     * Method to increase velocity by 1 after five rounds.
     */
    public void incVelocity(){
        if (velCount%5 == 0){
            setVelocity(getVelocity()+1);
        }
    }
    
    public int getVelocity(){return velocity;}
    public void setVelocity(int velocity){this.velocity = velocity;}
}
I also tried for loop to set the location but it seems even faster. So please how can I make the stickman jump realistically.
danpost danpost

2022/1/9

#
AbdulrazaqAS wrote...
I came across Stickman scenario with empty code so I am trying to write the code. I want the game to be the stickman jumping over enemies. I tried with this code block but it didn't work, the stickman will jump and come down within fractions of second. << Code Omitted >> I also tried for loop to set the location but it seems even faster. So please how can I make the stickman jump realistically.
You seem to have a misunderstanding on the basics of how greenfoot works. While your project is running, greenfoot will repeatedly call, in all, the act method of the world and all actors in the world, to create frames (or act steps). This is to be your animation loop provided for you. You program your world and actors for what they need to do at any one instance in time. A jump is a sequence of frames. You cannot say "if jumping, go up and them come back down". You need to say "apply gravity" and "fall (move vertically)" and "if touches ground while falling downward, stand" and "if standing and jump instructed, accelerate upward". I have a Jump and Run Demo w/Moving Platform scenario you can look at.
danpost danpost

2022/1/9

#
Just to be clear, using a for loop or Greenfoot.delay will not allow more frames to pass while your code runs. A for loop will complete whatever it does before the frame is set. A delay will temporarily pause your entire scenario -- not just that actor. Any code after the delay will process as well before the next frame is set. In either case, the entire process is done between act frames.
AbdulrazaqAS AbdulrazaqAS

2022/1/9

#
Thank you, I love that Jump and Run game. I will try and follow the your steps to make mine.
You need to login to post a reply.