Hi, I'm working on a platformer and I can't get the enemies to move.
This the code in the class itself
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Skeleton extends Enemies
{
int mapX;
int mapY;
int frame = 0;
private int animationCount;
private int vSpeed = 0;
private int acceleration = 2;
private int spriteHeight = getImage().getHeight();
private int spriteWidth = getImage().getWidth();
private int lookForGroundDistance = (int)spriteHeight/2;
private int lookForEdge = (int)spriteWidth/2;
private int speed = 1;
public Skeleton(int mapXIn, int mapYIn)
{
mapX = mapXIn;
mapY = mapYIn;
}
public void act()
{
checkFall();
move();
}
public void edgeDetection() {
if(getX() < 10 || getX() > getWorld().getWidth() - 10) {
speed *= -1;
}
}
public void move() {
Actor platform = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Platform.class);
if(platform == null) {
speed *= -1;
lookForEdge *= -1;
} else {
move(speed);
}
}
public void fall()
{
vSpeed += 1;
if (vSpeed >= 10) {
vSpeed = 10;
}
int dir=(int)Math.signum(vSpeed);
for(int step=0; step!=vSpeed; step+=dir)
{
setLocation(getX(), getY()+dir);
if(onPlatform())
{
setLocation(getX(), getY() - dir);
vSpeed = 0;
break;
}
}
}
public boolean onPlatform() {
Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 , Platform.class);
return under != null;
}
public boolean checkRightWalls()
{
int spriteWidth = getImage().getWidth();
int xDistance = (int)(spriteWidth/2);
Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
if(rightWall == null)
{
return false;
}
else
{
stopByRightWall(rightWall);
return true;
}
}
public void stopByRightWall(Actor rightWall)
{
int wallWidth = rightWall.getImage().getWidth();
int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2;
setLocation(newX - 5, getY());
speed *= -1;
}
public boolean checkLeftWalls()
{
int spriteWidth = getImage().getWidth();
int xDistance = (int)(spriteWidth/-2);
Actor leftWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
if(leftWall == null)
{
return false;
}
else
{
stopByLeftWall(leftWall);
return true;
}
}
public void stopByLeftWall(Actor leftWall)
{
int wallWidth = leftWall.getImage().getWidth();
int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2;
setLocation(newX + 5, getY());
speed *= -1;
}
public void checkFall() {
if(onPlatform()) {
vSpeed = 0;
}
else {
fall();
}
}
}
