import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class RobinsonStill here. * * @author (your name) * @version (a version number or a date) */ public class RobinsonStill extends Actor { public int vSpeed; public int gravity = 2; public boolean jumping; public int jumpStregth = 20; public int speed = 5; /** * Act - do whatever the RobinsonStill wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkFall(); checkKeys(); } public void checkKeys() { if(Greenfoot.isKeyDown("Up") && jumping == false) { jump(); } if(Greenfoot.isKeyDown("right")) { setLocation(getX() + speed, getY()); } /** if(Greenfoot.isKeyDown("left")) { setLocation(getY() + speed, getX()); } */ } public RobinsonStill() { GreenfootImage myImage = getImage(); int myNewHeight = (int)myImage.getHeight()/6; int myNewWidth = (int)myImage.getWidth()/6; myImage.scale(myNewWidth, myNewHeight); } public void checkFall() { if(onGround() == true) vSpeed = 0; else fall(); } public void fall() { setLocation(getX(), getY() + vSpeed); if(vSpeed <= 12) vSpeed = vSpeed + gravity; jumping = true; } public boolean onGround() { int spriteHeight = getImage().getHeight(); int lookForGround = spriteHeight/2; Actor ground = getOneObjectAtOffset(0, lookForGround, Floor.class ); if(ground == null) { jumping = true; return false; } else { moveToGround(ground); return true; } } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); jumping = false; } public void jump() { vSpeed = vSpeed - jumpStregth; jumping = true; fall(); } }