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

2014/8/28

How do you make an actor jump?

ILOVELABRADORS ILOVELABRADORS

2014/8/28

#
Hi. How do you make an actor jump? Thanks in advance.
danpost danpost

2014/8/29

#
First, you need to track the vertical velocity of the actor, or 'vSpeed'. Then, you need to give your actor gravity, a pull in the downward direction (add a small amount to 'vSpeed' every act cycle). Then, you need to find your actor to be standing on something that will negate the pull of gravity (standing on a surface). Finally, if standing on a surface and the trigger for jumping is detected, then subtract a large amount from 'vSpeed' (the force imparted by jumping).
// instance field
private int vSpeed;
// in act or a 'moveVertically' method that it calls
vSpeed++; // gravity 
setLocation(getX(), getY()+vSpeed); // vertical movement
if (vSpeed < 0) // if rising
{
    if (isTouching(null)) // bumps head
    {
        // adjustPosition under object
        vSpeed = 0;
    }
}
if (vSpeed > 0) // falling
{
    if (isTouching(null) || atWorldEdge()) // on surface
    {
        // adjust position over object or edge
        vSpeed = 0;
        if (/* jump trigger detected */)    vSpeed -= 15; // adjust value as needed
    }
}
ILOVELABRADORS ILOVELABRADORS

2014/8/29

#
I tried, but when I compiled it, it said the " private int vSpeed; " was an illegal start of expression. Any help?
danpost danpost

2014/8/29

#
Must be how you placed it within your code. Please show the class code.
ILOVELABRADORS ILOVELABRADORS

2014/8/29

#
// instance field
        private int vSpeed;
        // in act or a 'moveVertically' method that it calls
        vSpeed++; // gravity
        setLocation(getX(),getY()+vSpeed); // verical movement
        if (vSpeed < 0) // if rising
        {
            if (isTouching(null)) // bumps head
            {
                // adjustPosition under object
                vSpeed = 0;
            }
        }
        if (vSpeed > 0) // falling
        {
            if (isTouching(null) || atWorldEdge()) // on surface
            {
                // adjust position over object or edge
                vSpeed = 0;
                if (/* jump trigger detected */)    vSpeed = 15; //adjust value as needed
            }
        }   
    }
ILOVELABRADORS ILOVELABRADORS

2014/8/29

#
Was that what you wanted?
ILOVELABRADORS ILOVELABRADORS

2014/8/29

#
By the way, here's the whole 'moveAround' class...
public void moveAround()
    {
        if (Greenfoot.isKeyDown("A"))
        {
            move(-4);
        }
        
        if (Greenfoot.isKeyDown("D"))
        {
            move(4);
        }
        
        if (Greenfoot.isKeyDown("W"))
        {
            turn(-2);
        }    
        
        if (Greenfoot.isKeyDown("S"))
        {
            turn(2);
        }    
        
        if (Greenfoot.isKeyDown("1"))
        {
            setLocation(getX(), getY() - 8);
        }  
        
        // instance field
        private int vSpeed;
        // in act or a 'moveVertically' method that it calls
        vSpeed++; // gravity
        setLocation(getX(),getY()+vSpeed); // verical movement
        if (vSpeed < 0) // if rising
        {
            if (isTouching(null)) // bumps head
            {
                // adjustPosition under object
                vSpeed = 0;
            }
        }
        if (vSpeed > 0) // falling
        {
            if (isTouching(null) || atWorldEdge()) // on surface
            {
                // adjust position over object or edge
                vSpeed = 0;
                if (/* jump trigger detected */)    vSpeed -= 15; //adjust value as needed
            }
        }   
    }
danpost danpost

2014/8/29

#
Move line 28 and 29 to before line 1. An instance field is not declared inside the method; but outside of it; and it retains its value for the life of the actor (until it is no longer referenced and is flagged for garbage collection).
danpost danpost

2014/8/29

#
Lines 37 and 45 need to be replaced with the appropriate code for what the comments say they are for; and the 'if' condition on line 47 needs to be inserted also.
ILOVELABRADORS ILOVELABRADORS

2014/8/29

#
Okay, I'll try...
danpost danpost

2014/8/30

#
Your 'moveAround' method has the wrong type of movement for an actor that is supposed to be able to jump. You should not be using 'turn' and 'move'. The vertical movement is determined by surfaces and jumping; the horizontal movement should be adjustments to the position of the actor along the horizontal (changes in what 'getX()' returns) using the 'setLocation' method with detection of the 'A' and 'D' keys (obstructions should also be detected here). The 'W' key can be used for the trigger for jumping and the '1' and the 'S' key detection blocks can be removed.
You need to login to post a reply.