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

2024/5/26

how to make my character jump?

Nordrasil Nordrasil

2024/5/26

#
Hello I have a problem with my character because when I hold the "up" key to jump, my character goes to the top. While I would like him to do a jump and then come back down like in real life when I hold the "up" key. Thanks in advance to anyone who finds my error.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Mario1 here.
 * 
 * @author
 * @version (a version number or a date)
 */
public class GMario extends Mario
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private int speed;
    private int vSpeed;
    private int acceleration;
    private int jumpStrenght;
    private int animTimer;
    public GMario()
    {
        image1 = new GreenfootImage("marioR1.png");
        image2 = new GreenfootImage("marioL1.png");
        image3 = new GreenfootImage("marioR2.png");
        image4 = new GreenfootImage("marioL2.png");
        setImage(image1);
        speed = 3;
        vSpeed = 0;
        acceleration = 1;
        jumpStrenght = 2;
    }
    
    /**
     * Act - do whatever the Mario1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        checkPress();
        checkObstacle();
        checkFall();
        checkCollision();
        detection();
    }  
    
    private void checkPress()
    {
        int dx = 0;
        if (Greenfoot.isKeyDown("right")) 
        {
            dx++;
        }
        if (Greenfoot.isKeyDown("left")) 
        {
            dx--;
        }
        if (dx != 0) 
        {
            setLocation(getX()+dx*speed, getY());
            checkObstacle();
            animate(dx);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            vSpeed = -jumpStrenght;
            fall();
        }
    }
    
    private void animate(int dir) 
    {
        animTimer = (animTimer+1)%20;
        if (dir > 0) 
        {
            setImage(animTimer/10 == 0 ? image1 : image3);
        }
        else 
        {
            setImage(animTimer/10 == 0 ? image2 : image4);
        }
    }
    
    public void checkObstacle()
    {
        // check above the actor
        while (getOneObjectAtOffset(0, -getImage().getHeight()/2-1, Solid.class) != null) 
        {
            setLocation(getX(), getY()+1);
        }
        // check to left of actor
        while (getOneObjectAtOffset(getImage().getWidth()/2+1, 0, Solid.class) != null)
        {
            setLocation(getX()-1, getY());
        }
        // check to right of actor
        while (getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, Solid.class) != null)
        {
            setLocation(getX()+1, getY());
        }
    }
    
    public void jump()
    {
        vSpeed = -jumpStrenght;
        fall();
    }
    
    public void checkFall()
    {
        if(onSolid()) {
            vSpeed= 0;
        }
        else if (onPlateform()) {
            vSpeed= 0;
        }
        else {
            fall();
            if (getY() == 0)
            {
                Greenfoot.stop();
            }
        }
    } 
    
    public boolean onSolid() 
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2, 
        Solid.class);
        return under != null;
    }
    
    public boolean onPlateform()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2, 
        Plateform.class);
        return under != null;
    }
    
    private void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed < 10)
        {
            vSpeed = vSpeed + acceleration;
        }
    }
    
    public boolean onEnemies()
    {
        Actor under = getOneObjectAtOffset(getImage().getWidth()/2, getImage().getHeight()/2, Enemies.class);
        if( under != null)
        {
            return true;
        } 
        else 
        {
            return false;
        }
    }
    
    public void detection()
    {
        int distSautX = 0;
        int distSautY = 20;
        int w = getImage().getWidth()/2;
        int h = getImage().getHeight()/2;
        
        List<Enemies> myListEnemies=getWorld().getObjects(Enemies.class);
        
        for (Enemies myEnemy:myListEnemies)
        {
            if ((getX()> myEnemy.getX()- myEnemy.getImage().getWidth()/2 - distSautX - w) && (getX() < myEnemy.getX() + myEnemy.getImage().getWidth()/2 + distSautX + w))
            {
                if(getY() < myEnemy.getY() + myEnemy.getImage().getHeight()/2 + distSautY - h)
                {
                    getWorld().removeObject(myEnemy);
                    break;
                }
                //if(getY() >= myEnemy.getY() - myEnemy.getImage().getHeight()/2)
                //{
                //    getWorld().removeObject(this);
                //    break;
                //}
            }
        }
    }
    
    public void checkCollision()
    {
        if (isTouching(Coin.class)) 
        {
            removeTouching(Coin.class);
            World1 world1 = (World1)getWorld();
            world1.addScore(1);
        }
        if (isTouching(GMushroom.class))
        {
            removeTouching(GMushroom.class);
            World1 world1 = (World1)getWorld();
            world1.addLife(1); 
        }
        if (isTouching(RMushroom.class))
        {
            removeTouching(RMushroom.class);
            World1 world1 = (World1)getWorld();
        }
        if ( isTouching(Enemies.class) ) 
        {
            World1 world1 = (World1)getWorld();
            world1.addLife(-1); 
        }
    }    
}
Super_Hippo Super_Hippo

2024/5/26

#
Try to only let the character start a jump when they are currently not in the air.
danpost danpost

2024/5/27

#
You may want to increase the jump strength (line 30) to something closer to 12 or so.
Nordrasil Nordrasil

2024/5/27

#
It doesn't work. My character goes to the top of the screen faster than before.
danpost danpost

2024/5/28

#
Nordrasil wrote...
It doesn't work. My character goes to the top of the screen faster than before.
Remove lines 142, 143 and 145.
Nordrasil Nordrasil

2024/5/28

#
I don't know why but it still doesn't work.
danpost danpost

2024/5/29

#
Nordrasil wrote...
I don't know why but it still doesn't work.
I was going to suggest you having a look at the codes in my jump and run demo, but it does not explain how to add animation (it has only one image for left and only one image for right -- but, it also has image for jump {{ no help }} ). Even what it has is only commented in -- not implemented.
Nordrasil Nordrasil

2024/5/29

#
So, is there another way to make a "normal" jump with the Mario's animation ?
danpost danpost

2024/5/30

#
Nordrasil wrote...
So, is there another way to make a "normal" jump with the Mario's animation ?
Of course, but takes some doing. Best might be to put the animation sets in a double array (a 2 dimensional array where each row contains all the images for one specific animation -- right, left, whatever). To help, static final int fields can be added to "label" the rows:
private static final  int ANIM_RIGHT = 0, ANIM_LEFT = 1, ANIM_JUMP = 2;

private GreenfootImage[][] anims = new GreenfootImage[3][2];
private int curAnim;

public Mario() {
    String dir = "RL";
    for (int row=ANIM_RIGHT; row < 2; row++) {
        for (int frame=0; frame < 2; frame++) {
            anims[row][frame] = new GreenfootImage("mario"+dir.charAt(row)+(1+frame)+".png");
        }
    }
    setAnim(ANIM_RIGHT);
}

private void setAnim(int label) {
    if (curAnim != label) {
        curAnim = label;
        animate();
    }
}

private void animate() {
{
    animTimer = (animTimer+1)%20;
    setImage(anims[curAnim][animTimer/10]);
}
Just set the appropriate animate when left and right keys are detected. If jump animation is added, you will need to detect the landing to go back to a standing animation. Might need another field to remember if last standing left or right.
Nordrasil Nordrasil

2024/6/5

#
Thank you so much it's working !
You need to login to post a reply.