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

2012/5/25

how to jump

Benedict Benedict

2012/5/25

#
hey everyone, i´m kind of a noob in programming, because my teacher hasnt taught me anything so i dont know how to make a charater jump.
SPower SPower

2012/5/25

#
Create a gravity instance variable:
private int gravity;
Put this in act:
public void act()
{
   gravity--;
   setLocation(getX(), getY() - gravity);
   checkForJump();
}
You see there is a checkForJump() method. This is what the code will looklike, if you must jump if you see an object of the class Trampoline:
private void checkForJump()
{
    Actor a = getOneIntersectingObject(Trampoline.class);
    if (a != null) {
         gravity = 20; // this will make the character jump
    }
}
I hope this helps. If you don't understand something, just ask me.
Benedict Benedict

2012/5/25

#
Thank you very much i hope one day i can help you out :)
Jinice Jinice

2012/7/26

#
How can i make my character jump by pressing space bar?
danpost danpost

2012/7/26

#
@Jinice, Instead of lines 3 and 4 in the trampoline code above, use:
if (onGround && Greenfoot.isKeyDown("space"))
The boolean field 'onGround' should be declared as an instance variable in your character class, initially set to 'true'. Change to value to 'false' in the code block for the 'if' statement. When a 'landing' is detected, change it back to 'true'. Things to be aware of: detecting the landing could be a bit tricky; if your character is falling fast and the object to land on is not very thick (height-wise), the character could just pass it up or get stuck inside of it. What I prefer to do, is break each step of the fall into individual pixel-sized sub-steps using a 'for' loop and 'break'ing from it if a landing is detected (after resetting 'onGround' to 'true').
You need to login to post a reply.