I'm currently trying to recreate the rhythmic platformer game Geometry Dash in Greenfoot (if you are unfamiliar with the game, here is how it is supposed to work:
I haven't coded in the spikes yet, as I am having a few issues with the blocks. Whenever I land on a block, my character goes about halfway into the block instead of landing on the top of it like it's supposed to.
My player code:
As can be seen, I have already coded landing on the ground, and it works perfectly fine, so I am a bit confused as to why the blocks aren't working. Any help would be highly appreciated.
(P.S. both my character and the blocks have a file size of 30x30. The ground, which works, has a file size of 150x150)
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 | import greenfoot.*; public class Cube extends Actor { int direction = 5 ; int vSpeed = 0 ; int acceleration = 1 ; int jumpStrength = - 5 ; public void act() { checkKeys(); checkFall(); jump(); setLocation(getX()+direction,getY()); } public void checkKeys() { if (Greenfoot.isKeyDown( "space" )) { jump(); } } public void jump() { if (Greenfoot.isKeyDown( "space" ) && onBlock()) { vSpeed = jumpStrength; fall(); } if (Greenfoot.isKeyDown( "space" ) && onGround()) { vSpeed = jumpStrength; fall(); } } public void fall() { setLocation(getX(), getY()+vSpeed); vSpeed = vSpeed + acceleration; } public boolean onBlock() { Actor blockUnder = getOneObjectAtOffset ( 0 , getImage().getHeight()/ 2 , Block. class ); return blockUnder != null ; } public boolean onGround() { Actor groundUnder = getOneObjectAtOffset ( 0 , getImage().getHeight()/ 2 , Ground. class ); return groundUnder != null ; } public void checkFall() { if (onBlock()) { vSpeed = 0 ; } else if (onGround()) { vSpeed = 0 ; } else { fall(); } } } |