I wanted to create a platform game. So I have an Actor ladybug, and my goal is to get it to jump through this platform maze. However, I have two problems.
1. the ladybug twitches up and down a little even when it isn't moving
2. even though i developed the ceiling methods to stop the ladybug when it hits the ceiling, it can go through the ceilings.
Here is the code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ladybug3 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ladybug3 extends Actor
{
private int vSpeed=0;
private int acceleration=2;
private boolean jumping;
private int jumpStrength=17;
private int speed=5;
public void act()
{
checkFall();
checkKey();
setRotation(-90);
platformAbove();
// Add your action code here.
}
public void checkKey() {
if (Greenfoot.isKeyDown("space")&&jumping==false) {
jump();
}
if (Greenfoot.isKeyDown("right")){
{ setRotation(0);}
move(5);
}
if(Greenfoot.isKeyDown("left")){
{setRotation(180);}
move(5);
}
}
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)+5;
Actor ground=getOneObjectAtOffset(0, getImage().getHeight()/2, Bar2.class);
if(ground==null)
{
jumping=true;
return false;
}
else {
moveToGround(ground);
return true;
}
}
public boolean platformAbove()
{
int spriteHeight=getImage().getHeight();
int yDistance=(int)(spriteHeight/-2);
Actor ceiling=getOneObjectAtOffset(0, yDistance, Bar2.class);
if(ceiling !=null)
{
vSpeed=1;
bopHead(ceiling);
return true;
}
else {
return false;
}
}
public void bopHead(Actor ceiling) {
int ceilingHeight=ceiling.getImage().getHeight();
int newY=ceiling.getY()+(ceilingHeight+getImage().getHeight())/2;
setLocation(getX(), newY);
}
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 checkFall()
{
if(onGround()) {
vSpeed=0;
}
else {
fall();
}
}
public void jump() {
vSpeed=vSpeed-jumpStrength;
jumping=true;
fall();
}
}
