Hey guys, Im really new to Greenfoot with an assignement due soon....
I was wondering, I want one actor of mine to chase another actor
Lets say for example I want "Boo" to chase "Slimy", what do I need to do in order to make that happen.
void act(){
//this is Boo's act method
turnTowards(Slimy.getX(),Slimy.getY());
move(5);
}
Actor slimy = (Actor)getWorld().getObjects(Slimy.class).get(0);
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Slimy extends Actor
{
int value = 4;
int x,y;
int counter = 0;
int speed = 0;
public void act()
{
x = getX();
move();
y = getY();
if(Greenfoot.isKeyDown("left"))
{
setRotation(180);
move(value);
}
if(Greenfoot.isKeyDown("up"))
{
setRotation(270);
move(value);
}
if(Greenfoot.isKeyDown("down"))
{
setRotation(90);
move(value);
}
if(Greenfoot.isKeyDown("right"))
{
setRotation(0);
move(value);
}
if(isTouching(RedMushroom.class))
{
removeTouching(RedMushroom.class);
//Add 1 to the counter
counter++;
((Score)getWorld().getObjects(Score.class).get(0)).ScoreCounter(counter);
Greenfoot.playSound("Slurp.wav");
}
if(isTouching(GreenMushroom.class))
{
removeTouching(GreenMushroom.class);
//Add 1 to the counter
counter++;
((Score)getWorld().getObjects(Score.class).get(0)).ScoreCounter(counter);
Greenfoot.playSound("Slurp.wav");
}
if (getWorld() instanceof Level1)
if(counter>11)
{
Greenfoot.setWorld(new Level2());
}
if (getWorld() instanceof Level2)
if(counter>23)
{
Greenfoot.setWorld(new Level3());
}
if (isTouching(Boo.class))
{
Greenfoot.setWorld(new GameOverWorld());
}
}
private void move()
{
// get change in rotation
int dz = 0;
if (Greenfoot.isKeyDown("right")) dz++;
if (Greenfoot.isKeyDown("left")) dz--;
// apply change in rotation
setRotation(getRotation() + dz * 5);
// get change in speed
int ds = -1;
if (Greenfoot.isKeyDown("up")) ds += 20;
// adjust speed
speed += ds;
if (speed < 0) speed = 0;
if (speed > 2000) speed = 2000;
// and move
if (speed >= 200) move(speed / 100);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Boo extends Slimy
{
public void act()
{
turnTowards(Slimy.getX(),Slimy.getY());
move(5);
}
}