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

2013/4/27

Falling and jumping problem?

thekidj thekidj

2013/4/27

#
I followed the falling/ jumping tutorial I saw on youtube and I can't get my actor to just jump once (no double, triple, quadruple, etc.) and he keeps falling through the platforms that I have created. Anything stand out to you guys?
public class Giovanni extends Actor
{
    private GreenfootImage giovanni_normal;
    private GreenfootImage giovanni_walk_right;
    private GreenfootImage giovanni_walk_right2;
    private GreenfootImage giovanni_walk_left;
    private GreenfootImage giovanni_walk_left2;
    private int speed = 2; //movement speed
    private int vSpeed = 0; //vertical speed
    private int acceleration = 2; //gravity effect while falling
    private int jumpStrength = -8;

    public Giovanni()
    {
        giovanni_normal = new GreenfootImage ("Stand.png");
        giovanni_walk_right = new GreenfootImage ("Right.png");
        giovanni_walk_right2 = new GreenfootImage ("Right2.png");
        giovanni_walk_left = new GreenfootImage ("Left.png");
        giovanni_walk_left2 = new GreenfootImage ("Left2.png");
    }

    /**
     * Act - do whatever the Giovanni wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        jump();
        checkFall();
    }    

    public void move()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+speed,getY());
            if(getImage() != giovanni_walk_right)
            {
                setImage(giovanni_walk_right);
            }
            else if (getImage() != giovanni_walk_right2)    
            {
                setImage(giovanni_walk_right2);
            }
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-speed,getY());
            if(getImage() != giovanni_walk_left)
            {
                setImage(giovanni_walk_left);
            }
            else if (getImage() != giovanni_walk_right2)      
            {
                setImage(giovanni_walk_left2);
            }
        }
        if (!Greenfoot.isKeyDown("right") && (!Greenfoot.isKeyDown("left")))
        {
            setImage(giovanni_normal);
        }
    }

    public void jump()
    {
        if (Greenfoot.isKeyDown("space"))
        {
            vSpeed = jumpStrength;
            fall();
        }
    }

    public void fall()
    {
        setLocation(getX(), getY()+vSpeed);
        vSpeed = vSpeed + acceleration;
    }

    public boolean onPlatform()
    {
        Actor under = getOneObjectAtOffset (0, getImage().getHeight()/2, Platform.class);
        return under != null;
    }

    public void checkFall()
    {
        if (onPlatform())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
}
danpost danpost

2013/4/27

#
You do not have anything regulating the jump routine. Basically, what the 'jump' method says at the moment is this: "as long as the space key is pressed, maintain jump speed." You should have a condition or two regulating it: (1) the actor should be on a surface (ground or platform) to jump; and (optional) (2) the space key must be released and the actor on ground or surface before a jump can be executed again. Once you control the jump, then you can work on the problem of falling through the platform (if it is still a problem).
thekidj thekidj

2013/4/27

#
danpost wrote...
You do not have anything regulating the jump routine. Basically, what the 'jump' method says at the moment is this: "as long as the space key is pressed, maintain jump speed." You should have a condition or two regulating it: (1) the actor should be on a surface (ground or platform) to jump; and (optional) (2) the space key must be released and the actor on ground or surface before a jump can be executed again. Once you control the jump, then you can work on the problem of falling through the platform (if it is still a problem).
So I should have if (Greenfoot.isKeyDown("space") && (onPlatform())) then the body of my jump method?
thekidj thekidj

2013/4/27

#
danpost wrote...
You do not have anything regulating the jump routine. Basically, what the 'jump' method says at the moment is this: "as long as the space key is pressed, maintain jump speed." You should have a condition or two regulating it: (1) the actor should be on a surface (ground or platform) to jump; and (optional) (2) the space key must be released and the actor on ground or surface before a jump can be executed again. Once you control the jump, then you can work on the problem of falling through the platform (if it is still a problem).
disregard my last post, that worked! thanks. he's no longer falling through the platform either. woops, disregard that. the jumping issue is fixed but the actor is still going through the platform
danpost danpost

2013/4/27

#
You need to be checking for ground or platform while the actor 'fall's. When detected, place him on the surface. Use the 'getOneIntersectingObject' method, then determine if the object is above or below the actor and place the actor up against or set upon the surface of the intersector and set vSpeed to zero.
thekidj thekidj

2013/4/27

#
danpost wrote...
You need to be checking for ground or platform while the actor 'fall's. When detected, place him on the surface. Use the 'getOneIntersectingObject' method, then determine if the object is above or below the actor and place the actor up against or set upon the surface of the intersector and set vSpeed to zero.
Isn't that what my checkFall method is doing?
danpost danpost

2013/4/27

#
thekidj wrote...
Isn't that what my checkFall method is doing?
Not really. The 'checkFall' method is very specific as to what it does. It more or less checks to see if you are already standing on a Platform object. It checks only at the foot of the actor for a platform object. While 'fall'ing, your actor could be going upwards and hit his head on a platform or it could be going downward and in one act cycle be barely above the platform, and the next, half past the platform and the 'onPlatform' method will not detect the platform at the actors feet (hence, your actor will fall through). Or, maybe it does detect it, but your actor is now stuck half-way through the platform. These situations have to be dealt with, so what I said above should help in the correcting of them.
thekidj thekidj

2013/4/27

#
So I'd have if the following
Actor thePlatform = getOneIntersectingObject( Platform.class );
if ( thePlatform != null )
{
setLocation( ???, getY());
}
I'm not sure what I'd put in for X
You need to login to post a reply.