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

2013/6/12

Jumping

1
2
Gingervitis Gingervitis

2013/6/12

#
Is it possible to make an actor jump but instead of going up and falling, you jump down and fall up?
Gevater_Tod4711 Gevater_Tod4711

2013/6/12

#
Of course this is possible. You just need to change the gravity engine so that the gravity makes the objects go up (instead of y + gravity use y - gravity). And your actor should jump down not up. But therefore you also just need to change the moving direction.
Gingervitis Gingervitis

2013/6/12

#
Ok. Let me try what you said. If I can't get it to work I will post the code.
Gevater_Tod4711 Gevater_Tod4711

2013/6/12

#
Maybe this helps you.
Gingervitis Gingervitis

2013/6/12

#
I am modifying the pengu game but I can't seem to get it to work. Here is what the mover class looks like....
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * The class Mover provides some basic movement methods. Use this as a superclass
 * for other actors that should be able to move left and right, jump up and fall 
 * down.
 */
public class Mover extends Actor
{
    private static final int acceleration = 2;      // down (gravity)
    private static final int speed = 7;             // running speed (sideways)
    
    private int vSpeed = 1;                         // current vertical speed
    

    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
    
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2-8 , null);
        return under != null;
    }

    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }
    
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed - acceleration;
        if ( atBottom() )
            gameEnd();
    }
    
    private boolean atBottom()
    {
        return getY() >= getWorld().getHeight() + 2;
    }
    
    private void gameEnd()
    {
        Greenfoot.stop();
    }


}
Gevater_Tod4711 Gevater_Tod4711

2013/6/12

#
In line 28 you should use -getImage().getHeight()/2-8 to check if there is a ground object abouve you. In line 39 you should use - vSpeed instead of + vSpeed and in line 40 use + acceleartion. I think then it should work.
Gingervitis Gingervitis

2013/6/12

#
The code in the link you posted is very complex and confusing. Is there a way to modify the code I previously posted to get it to jump oppositely?
Gingervitis Gingervitis

2013/6/12

#
Oh ok let me try it out.
Gingervitis Gingervitis

2013/6/12

#
When I added that the pengu just floats to the top and the jump doesn't work. Here is pengu class.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A little penguin that wants to get to the other side.
 */
public class Pengu extends Mover
{
    private static final int jumpStrength = 16;
    
    public void act() 
    {
        checkKeys();        
        checkFall();
    }
    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("pengu-left.png");
            moveLeft();
        }
        if (Greenfoot.isKeyDown("right") )
        {
            setImage("pengu-right.png");
            moveRight();
        }
        if (Greenfoot.isKeyDown("space") )
        {
            if (onGround())
                jump();
        }
    }    
    
    private void jump()
    {
        setVSpeed(-jumpStrength);
        fall();
    }
    
    private void checkFall()
    {
        if (onGround()) 
        {
            setVSpeed(0);
        }
        else 
        {
            fall();
        }
    }


}

    
Gevater_Tod4711 Gevater_Tod4711

2013/6/12

#
In line 37 you should use setVSpeed(jumpStrength) instead of - jumpStrength. Otherwhile the Pengu will jump up (or try to jump up).
Gingervitis Gingervitis

2013/6/12

#
I tried that before but nothing happened....
Gevater_Tod4711 Gevater_Tod4711

2013/6/12

#
Forget about my last reply -jumpStrength was right I think. The problem probably is that vSpeed is increased every act and so it's value is so much that you can't see the jump. Can you check the value for vSpeed when you jump? (using the inspect window of greenfoot).
Gingervitis Gingervitis

2013/6/12

#
The vSpeed just keeps increasing
Gevater_Tod4711 Gevater_Tod4711

2013/6/12

#
The only thing I could imagine is that the jump is so small that you just can't see it. I don't see any mistakes in the code.
Gingervitis Gingervitis

2013/6/12

#
Would it work if the acceleration was bigger?
There are more replies on the next page.
1
2