how would i code an enemy to move left but when it reaches the edge of the platform its on that it starts moving to the right and when it hits the edge that it turns left and repeats this process
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class HealthBar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HealthBar extends Actor
{ int health=5;
int healthBarWidth=100;
int healthBarHeight=20;
int pixelsPerHealthPoint=healthBarWidth/health;
/**
* Act - do whatever the HealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public HealthBar()
{
update();
}
public void act()
{
update();
Loser();
}
public void update()
{
setImage(new GreenfootImage(healthBarWidth+2,healthBarHeight+2));
getImage().setColor(Color.BLACK);
getImage().drawRect(0,0, healthBarWidth+1,healthBarHeight+1);
getImage().setColor(Color.RED);
getImage().fillRect(1,1,health*pixelsPerHealthPoint,healthBarHeight);
}
public void Loser()
{
if(health==0)
{
Greenfoot.setWorld(new LoserScreen());
Greenfoot.stop();
}
}
}
/**
* Write a description of class Bee here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bee extends Actor
{
int health = 100; // Current health
int maxHealth = 100;
int healthBarWidth = 100; // Physical width in pixels
int healthBarHeight = 15;
int pixelsPerHealthPoint= healthBarWidth / maxHealth;
boolean hitRock=false;
/**
* Act - do whatever the Earth wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
hitRocks();
playerControls();
}
public void loseHealth(int amount)
{
health -= amount;
if (health < 0) health = 0;
}
public void hitRocks()
{
if(isTouching(Poison.class) && hitRock == false)
{
MyWorld myWorld= (MyWorld) getWorld();
HealthBar healthbar=myWorld.getHealthBar();
healthbar.health--;
hitRock = true;
}
else if (!isTouching(Poison.class))
{
hitRock=false;
}
}
public void playerControls()
{
//Moves Crab up and down
if (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("W"))
{
setLocation(getX(), getY() - 5);
}
if (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("S"))
{
setLocation(getX(), getY() + 5);
}
//Moves Crab left and right
if (Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("D"))
{
setLocation(getX() +5, getY());
}
if (Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("A"))
{
setLocation(getX() -5, getY());
}
}
}
/**
* Write a description of class Bee here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bee extends Actor
{
int health = 100; // Current health
int maxHealth = 100;
int healthBarWidth = 100; // Physical width in pixels
int healthBarHeight = 15;
int pixelsPerHealthPoint= healthBarWidth / maxHealth;
boolean hitRock=false;
/**
* Act - do whatever the Earth wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
hitRocks();
playerControls();
}
public void loseHealth(int amount)
{
health -= amount;
if (health < 0) health = 0;
}
public void hitRocks()
{
if(isTouching(Poison.class) && hitRock == false)
{
MyWorld myWorld= (MyWorld) getWorld();
HealthBar healthbar=myWorld.getHealthBar();
healthbar.health--;
hitRock = true;
}
else if (!isTouching(Poison.class))
{
hitRock=false;
}
}
public void playerControls()
{
//Moves Crab up and down
if (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("W"))
{
setLocation(getX(), getY() - 5);
}
if (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("S"))
{
setLocation(getX(), getY() + 5);
}
//Moves Crab left and right
if (Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("D"))
{
setLocation(getX() +5, getY());
}
if (Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("A"))
{
setLocation(getX() -5, getY());
}
}
}
/**
* Write a description of class Poison here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Poison extends Actor
{
private int timer = 0;
/**
* Act - do whatever the Poison wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Poison()
{
getImage().fillRect(0, 0, 1200, 10);
GreenfootImage img=new GreenfootImage(1200,10);
img.setColor(Color.RED);
img.fill();
setImage(img);
}
public void act()
{
//Every second the poison rises
timer ++;
if (timer >= 60)
{
rise();
timer = 0;
}
}
public void rise()
{
//actually rises
int currentX = getX();
int currentY = getY();
int upwardSpeed = 5;
setLocation(currentX, currentY - upwardSpeed);
//so that is the poison rises, the image does so its not just a straight line going up the screen
getImage().scale(getImage().getWidth()+0, getImage().getHeight()+10);
}
}int health = 100; // or 5, or whatever
HealthBar healthbar; // each bee retains a reference to its health bar object
public Bee() {
healthbar = new BeeHealthBar(100, 15, health); // dimensions (width and height) and maximum health value given as parameters
}
// add health bar to world when Bee object is added to the world
protected void addedToWorld(World world) {
world.addObject(healthbar, getX(), getY()-getImage().getHeight()/2-5); // given positional offset is retained in health bar object
}
/** the following is an extension of the HealthBar class, giving health bars
** the ability to move with the object it was created for
*/
protected class BeeHealthBar extends HealthBar
{
int offsetX, offsetY; // positional offsets to Bee object it belongs to
public BeeHealthBar(int width, int height, int parts) {
healthBarWidth = width;
healthBarHeight = height;
pixelsPerHealthPoint = width/parts;
width = width-(width%pixels); // removing excess pixels
}
// save positional offsets when added into world
protected void addedToWorld(World world) {
offsetX = getX()-Bee.this.getX();
offsetY = getY()-Bee.this.getY();
}
public void act() {
if (Bee.this.getWorld() == null) {
getWorld().removeObject(this);
return;
}
positionSelf();
super.act();
}
protected void positionSelf() {
setLocation(Bee.this.getX()+offsetX, Bee.this.getY()+offsetY);
}
}import greenfoot.*;
public class Poison extends Actor
{
private int timer = 0;
private int growth = 2;
protected void addedToWorld(World world) {
updateImage();
}
private void updateImage() {
GreenfootImage img = new GreenfootImage(getWorld().getWidth(), 4*growth);
img.setColor(Color.RED);
img.fill();
setImage(img);
setLocation(getWorld().getWidth()/2, getWorld().getHeight()-2*growth);
}
public void act() {
timer ++;
if (timer >= 60) {
growth++;
updateImage();
timer = 0;
}
}
}