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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 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(); } } |

