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

2013/3/29

Jump code..

1
2
JetLennit JetLennit

2013/3/29

#
How can i make a player have to go back down after he jumps? (for my game runner)
danpost danpost

2013/3/29

#
To initially jump, you add a negative value to the current speed in the vertical direction (maybe something like -15). The current vertical speed is the amount you will move the player in the y direction each act method. If you add one to that value each act cycle, the actor will slow down as it goes up, momentarily stop at the apex, and then slowly pick up speed in the downward direction.
JetLennit JetLennit

2013/3/29

#
Im confused.......
danpost danpost

2013/3/30

#
A simple jump/gravity code would be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import greenfoot.*
 
public class Jumper extends Actor
{
    private int ySpeed;
 
    public Jumper()
    {
    }
 
    public void act()
    {
        int groundLevel = getWorld().getWidth() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            ySpeed++; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground
                Greenfoot.getKey(); // clears any key pressed during jump
           }
        }
        else // on ground
        {
            if ("space".equals(Greenfoot.getKey())) // jump key detected
            {
                ySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed; // leave ground
            }
        }
    }
}
JetLennit JetLennit

2013/3/30

#
it doesn't appear to work.......................................................
danpost danpost

2013/3/30

#
Yeah. Make the following changes
1
2
3
4
5
6
// add a semi-colon at the end of line 1
import greenfoot.*;
// change 'getWidth()' to 'getHeight()' in line 13
int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
// add the missing close parenthesis at the end of line 30
setLocation(getX(), getY()+ySpeed);
Sorry, I wrote it hastily.
JetLennit JetLennit

2013/3/30

#
How do i make it to where it stays in the air a bit longer?
danpost danpost

2013/3/30

#
To make it jump higher, change the value in line 29 to a larger negative.
JetLennit JetLennit

2013/3/30

#
I dont need it higher... i need it not to fall for longer
danpost danpost

2013/3/30

#
You will need to add an instance 'apexTimer' field to hold the actor at the apex for a pre-determine amount of time. The class code would look like this now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import greenfoot.*;
 
public class Jumper extends Actor
{
    private int ySpeed;
    private int apexTimer;
 
    public Jumper()
    {
    }
 
    public void act()
    {
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            if (ySpeed == 0 && apexTimer > 0) apexTimer--; // run apex timer
            if (ySpeed == 0 && apexTimer > 0) return; // apex timer still running
            ySpeed++; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground
                Greenfoot.getKey(); // clears any key pressed during jump
           }
        }
        else // on ground
        {
            if ("space".equals(Greenfoot.getKey())) // jump key detected
            {
                ySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave ground
                apexTimer = 100// set apex timer (adjust value to suit)
            }
        }
    }
}
JetLennit JetLennit

2013/3/30

#
thank you sooo much! it has worked exactly as i wanted!
danpost danpost

2013/3/30

#
Glad to be of some help.
JetLennit JetLennit

2013/4/13

#
How do i change that code to where if Ground.class is below it it won't fall?
danpost danpost

2013/4/13

#
You could change line 23 to loop through the falling and check for Ground at each location along the fall. As an alternative, you can extend the bottom of the Ground image, increasing the height of the image, so that the falling will not 'skip' over the image.
JetLennit JetLennit

2013/4/14

#
can you show me the code for that? (I don't fully understand what you mean)
There are more replies on the next page.
1
2