How to make my actor so he can jump not only on the ground.class(platforms) but also on the bottom of the background thank you.
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Vilkas here. * * @author (your name) * @version (a version number or a date) */ public class Vilkas extends Actor { private GreenfootImage image1 = new GreenfootImage( "wombat.png" ); private GreenfootImage image2 = new GreenfootImage ( "wombat-left.png" ); private int vSpeed = 0 ; private int acceleration = 2 ; private boolean jumping; private int jumpStrength = 20 ; public void act() { checkFall(); animate(); CheckKeys(); platformAbove(); checkRightWalls(); checkLeftWalls(); } public void CheckKeys() { if (Greenfoot.isKeyDown( "a" )){ move(- 5 ); } if (Greenfoot.isKeyDown( "d" )){ move( 5 ); } if (Greenfoot.isKeyDown( "space" ) && jumping == false ){ jump(); } } public void jump() { vSpeed = vSpeed - jumpStrength; jumping = true ; fall(); } public void animate() { if ((Greenfoot.isKeyDown( "d" ))){ setImage (image1); } if ((Greenfoot.isKeyDown( "a" ))){ setImage (image2); } } public void fall() { setLocation(getX(),getY()+ vSpeed); if (vSpeed <= 9 ) { vSpeed = vSpeed + acceleration; } jumping = true ; } public boolean onGround() { int spriteHeight = getImage().getHeight(); int yDistance = ( int ) (spriteHeight / 2 + 50 ); Actor ground = getOneObjectAtOffset ( 0 ,getImage().getHeight() / 2 , Ground. class ); if (ground == null ) { jumping = true ; return false ; } else { moveToGround(ground); return true ; } } public void checkFall() { if (onGround()) { vSpeed= 0 ; } else { fall(); } } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight()) / 2 ; setLocation(getX(),newY); jumping = false ; } public boolean platformAbove() { int spriteHeight = getImage().getHeight(); int yDistance = ( int )(spriteHeight/- 2 ); Actor ceiling = getOneObjectAtOffset( 0 , yDistance, Ground. class ); if (ceiling != null ) { vSpeed = 1 ; bopHead(ceiling); return true ; } else { return false ; } } public boolean checkRightWalls() { int spriteWidth = getImage().getWidth(); int xDistance = ( int ) (spriteWidth/ 2 ); Actor rightWall = getOneObjectAtOffset(xDistance, 0 ,Ground. class ); if (rightWall == null ) { return false ; } else { stopByRightWall(rightWall); return true ; } } public void stopByRightWall (Actor rightWall) { int wallWidth = rightWall.getImage().getWidth(); int newX = rightWall.getX() - (wallWidth + getImage().getWidth()) / 2 ; setLocation(newX - 5 , getY()); } public void bopHead(Actor ceiling) { int ceilingHeight = ceiling.getImage().getHeight(); int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/ 2 ; setLocation(getX(), newY); } public boolean checkLeftWalls() { int spriteWidth = getImage().getWidth(); int xDistance = ( int ) (spriteWidth/ 2 ); Actor leftWall = getOneObjectAtOffset(-xDistance, 0 ,Ground. class ); if (leftWall == null ) { return false ; } else { stopByleftWall(leftWall); return true ; } } public void stopByleftWall (Actor leftWall) { int wallWidth = leftWall.getImage().getWidth(); int newX = leftWall.getX() + (wallWidth + getImage().getWidth()) / 2 ; setLocation(newX + 5 , getY()); } } |