I'm making a side-scrolling ninja turtle game and my actor keeps going through the platforms from the bottom but stays on it when it goes from the top.
How do I make the actor fall back down when it hits its head on the bottom of the platform? (First game by the way and I've got 6 hours left to finish it, please help)
The character (Mikey):
The platform(red bricks):
Background:
public class Mikey extends Actor { private int upSpeed = 0; private int accelerataion = 1; private int jumpDistance= -20; private int collect = 0; public Mikey() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 440, image.getHeight() - 490); setImage(image); } /** * Act - do whatever the Mikey wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveMikey(); checkFalling(); collect(); } private void fall() { setLocation(getX(), getY() + upSpeed); upSpeed = upSpeed + accelerataion; } public void moveMikey() { if(Greenfoot.isKeyDown("right")) { move(6); } if(Greenfoot.isKeyDown("left")) { move(-6); } if (Greenfoot.isKeyDown("space")&&(onPlatform()==true || onGround() == true)) { upSpeed = jumpDistance; fall(); } } boolean onPlatform() { Actor bottom = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class); return bottom != null; } boolean onGround() { Actor bottom = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class); return bottom != null; } public void checkFalling() { if (onPlatform()== false|| onGround() == true) { fall(); } if (onPlatform() == true|| onGround() == true) { upSpeed = 0; } } }
public class Platform extends Actor { public Platform() { getImage().scale(getImage().getWidth()*2,getImage().getHeight()/4); } /** * Act - do whatever the Floors wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move (-2); if (getX() == 0) { setLocation(getWorld().getWidth() - 1, getY()); } } }
public class Background extends World { /** * Constructor for objects of class Background. * */ public Background() { super(1080, 550, 1); prepare(); } private void prepare() { Building building = new Building(); addObject(building,1526,192); building.setLocation(1009,280); Platform platform = new Platform(); addObject(platform,830,120); Platform platform2 = new Platform(); addObject(platform2,189,138); Platform platform3 = new Platform(); addObject(platform3,534,302); Platform platform4 = new Platform(); addObject(platform4,846,435); Platform platform5 = new Platform(); addObject(platform5,213,462); Platform platform6 = new Platform(); addObject(platform6,958,341); platform3.setLocation(635,291); platform4.setLocation(871,129); platform5.setLocation(898,419); platform2.setLocation(351,443); platform.setLocation(413,120); platform6.setLocation(140,233); Mikey mikey = new Mikey(); addObject(mikey,315,297); mikey.setLocation(58,423); } }