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)
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();
}
}
}

