I was just wondering how to make a actor jump. I have seen it done in levels but i never could figure it out.


private int gravity = 0;
setLocation(getX(), getY() -gravity); gravity--;
if (mustJump() ) { gravity = 20; }
public void act() { //fill this in with what you want, //but add this here: if(mustJump()) jump(); checkFall(); } private void checkFall() { if(!onGround()) fall(); } private boolean onGround() { int myHeight = getImage().getHeight(); Actor ground = getOneObjectAtOffset(0, myHeight/2, Ground.class); if(ground != null)//if the ground is really there... return true; //(if not, return false) return false; } //this is quite like SPower's method private void fall() { setLocation(getX(), getY() + gravity); gravity++;//the player will accelerate as he falls } private void jump() { if(onGround()) gravity -= 20;//or choose 10 or 15 or whatever works }
if ( mustJump() )
onGround && Greenfoot.isKeyDown("up")
public void act() { //fill this in with what you want, //but add this here: if(onGround && (Greenfoot.isKeyDown("up"))) jump(); checkFall(); } private void checkFall() { if(!onGround()) fall(); } private boolean onGround() { int myHeight = getImage().getHeight(); Actor ground = getOneObjectAtOffset(0, myHeight/2, Ground.class); if(ground != null)//if the ground is really there... return true; //(if not, return false) return false; } //this is quite like SPower's method private void fall() { setLocation(getX(), getY() + gravity); gravity++;//the player will accelerate as he falls } private void jump() { if(onGround()) gravity -= 20;//or choose 10 or 15 or whatever works } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class actor here. * * @author (your name) * @version (a version number or a date) */ public class actor extends Actor { /** * Act - do whatever the actor wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ private int gravity = 0; public void act() { //fill this in with what you want, //but add this here: if(onGround() && (Greenfoot.isKeyDown("up"))) jump(); checkFall(); } private void checkFall() { if(!onGround()) fall(); } private boolean onGround() { int myHeight = getImage().getHeight(); Actor ground = getOneObjectAtOffset(0, myHeight/2, Ground.class); if(ground != null)//if the ground is really there... return true; //(if not, return false) return false; } //this is quite like SPower's method private void fall() { setLocation(getX(), getY() + gravity); gravity++;//the player will accelerate as he falls } private void jump() { gravity -= 20;//or choose 10 or 15 or whatever works } }