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

2020/10/24

My Characters wont jump for some reason

MrSkyPanda MrSkyPanda

2020/10/24

#
I figured out the gravity thing but now they can't jump. Please help
int vSpeed = 0;
    int accel = 0;
    private int gravity;
    public void act() 
    {
        checkFalling();
        fall();
        gravity--;
        setLocation(getX(), getY());
        checkForJump();
        
        if(Greenfoot.isKeyDown("left")) {
            move(-3);
        }        
        if(Greenfoot.isKeyDown("right")) {
            move(3);
        }   // Add your action code here.
        if(!isTouching(Wide.class))
        {
            vSpeed++;
        }
        else
            vSpeed = 0;
    }  
    
    public void fall()
    {
        
        setLocation(getX(), getY() + vSpeed);

    }
    
    public void checkFalling()
    {
        if(!isTouching(Wide.class))
        {
            vSpeed++;
        }
        else
            vSpeed = 0;
    }
    public void jump()
    {
        if(Greenfoot.isKeyDown("up"))
        {
            vSpeed = -10;
        }
    }
    private void checkForJump()
    {
        if (Greenfoot.isKeyDown("up"))
        { 
            gravity = 20; // this will make the character jump
        }

    }   
    public void death()
    {
            if(!isTouching(Enemies.class))
            {
                   Greenfoot.playSound("death.mp3");
            }
    

    }   
RcCookie RcCookie

2020/10/28

#
You aren’t calling the jump() method, only the checkJump() method. You generally don’t need the gravity variable. If you want to be able to adjust the gravity strength you need to adjust how much vSpeed is changed when the object is falling
You need to login to post a reply.