I am making a side-scrolling ninja game and I want to make the bottomFloor something that the actor cant fall through. Right now his body stays where it was before and the image I added as the bottomFloor just overlaps it and does nothing. How could I make it so that the character stands on it as if its the bottom of the screen?
Current Character class:
BottomFloor that I dont want the character to sink into:
Thanks :)
public class Mikey extends Actor { private int gravity = 1; private int step = 7; private int velocity = 0; private int collect = 0; public Mikey() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 460, image.getHeight() - 510); setImage(image); } public void act() { fall(); move(); collect(); if (Greenfoot.isKeyDown("space") && isOnSolidPlatform()) { jump(); } } public void moveMikey() { if(Greenfoot.isKeyDown("right")) { move(6); } if(Greenfoot.isKeyDown("left")) { move(-6); } } public void fall() { setLocation(getX(), getY() + velocity); if(isOnSolidPlatform()) { velocity = 0; while (isOnSolidPlatform()) { setLocation(getX(), getY() -1); } setLocation(getX(), getY() +1); } else if (velocity < 0 && didBumpHead()) { velocity = 0; while(didBumpHead()) { setLocation(getX(), getY() + 1); } } else { velocity = velocity + gravity; } } public void jump() { velocity = -20; } public void move() { int y = getY(); int x = getX(); if (Greenfoot.isKeyDown("left") && canMoveLeft()) { x = x - step; } if (Greenfoot.isKeyDown("right") && canMoveRight()) { x = x + step; } setLocation(x,y); } public boolean isOnSolidPlatform() { boolean isOnPlatform = false; if (getY() > getWorld().getHeight() - 50) { isOnPlatform = true; } if (getOneObjectAtOffset(getImage().getWidth() / -2, getImage().getHeight() / 2, Platform.class) != null || getOneObjectAtOffset(getImage().getWidth() / 2, getImage().getHeight() / 2, Platform.class) != null) { isOnPlatform = true; } //add smth like this so that mikey dont fall down under ground return isOnPlatform; } public boolean didBumpHead() { boolean bumpedHead = false; if (getOneObjectAtOffset(getImage().getWidth()/ -2, getImage().getHeight() / -2, Platform.class) != null || getOneObjectAtOffset (getImage().getWidth() / 2, getImage().getHeight() / -2, Platform.class) != null) { bumpedHead = true; } return bumpedHead; } public boolean canMoveLeft() { boolean canMoveLeft = true; if (getOneObjectAtOffset(getImage().getWidth()/ -6 - step, getImage().getHeight() / -6, Platform.class) != null || getOneObjectAtOffset (getImage().getWidth() / -6 - step, getImage().getHeight() / 6 - 1, Platform.class) != null) { canMoveLeft = false; } return canMoveLeft; } public boolean canMoveRight() { boolean canMoveLeft = true; if (getOneObjectAtOffset(getImage().getWidth()/ 6 + step, getImage().getHeight() / -6, Platform.class) != null || getOneObjectAtOffset (getImage().getWidth() / 6 + step, getImage().getHeight() / 6 - 1, Platform.class) != null) { canMoveLeft = false; } return canMoveLeft; } public void collect() { Actor pizza = getOneIntersectingObject(Pizza.class); if (pizza != null) { getWorld().removeObject(pizza); collect = collect + 1; } if (collect == 100) { Greenfoot.stop(); } } public boolean onGround() { Actor bottom = getOneObjectAtOffset(0, getImage().getHeight()/2, bottomFloor.class); return bottom != null; } }
public class bottomFloor extends Actor { public bottomFloor() { getImage().scale(getImage().getWidth()*4,getImage().getHeight()/3); } public void act() { // Add } }