I am trying to get the bubbles to follow the butterfly around.
Here is the butterfly code which isn't very unique and has
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public class Bubble extends Bubbles
{
//float around, does not do damage lvl 1
private int currentSteps = 0;
private Butterfly myButterfly;
public void act()
{
turnAtEdge();
randomTurn();
move(2);
if (getWorld().getObjects(Butterfly.class).isEmpty()) return; // skips following if the tank is not in world
Actor tank = (Actor)getWorld().getObjects(Butterfly.class).get(0); // gets reference to tank
turnTowards(Butterfly.getX(), Butterfly.getY()); // turn toward tank]
}
/**
* Check whether we are at the edge of the world. If we are, turn a bit.
* If not, do nothing.
* Only level 2 & 3 bubble should move
*/
public void turnAtEdge()
{
if ( atWorldEdge() )
{
turn(17);
}
}
/**
* Randomly decide to turn from the current direction, or not. If we turn
* turn a bit left or right by a random degree.
*/
public void randomTurn()
{
if(Greenfoot.getRandomNumber(100) > 90) {
turn(Greenfoot.getRandomNumber(90)-45);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public class Butterfly extends Mover
{
private int gunReloadTime; // The minimum delay between firing the gun.
private int reloadDelayCount; // How long ago we fired the gun the last time.
private Vector acceleration; // How fast the rocket is.
private int shotsFired; // Number of shots fired.
int collisionCountdown = 0;
private GreenfootImage rocket = new GreenfootImage("butterfly.png");
private GreenfootImage rocketWithThrust = new GreenfootImage("butterflyWithThrust.png");
/**
* Initilise this rocket.
*/
public Butterfly()
{
gunReloadTime = 10;
reloadDelayCount = 0;
acceleration = new Vector(0, 0.01);
increaseSpeed(new Vector(4, 0)); // initially stopped
shotsFired = 0;
}
public void act()
{
move();
checkKeys();
reloadDelayCount++;
if( collisionCountdown > 0 )
{
collisionCountdown--;
}
else
{
handleCollisions();
}
}
/**
* shots fired
*/
public int getShotsFired()
{
return shotsFired;
}
/**
* slowed down
*/
public int getSpeed()
{
return (int) (getMovement().getLength() * 5);
}
/**
* Set the time needed for re-loading the rocket's gun. The shorter this time is,
* the faster the rocket can fire. The (initial) standard time is 20.
*/
public void setGunReloadTime(int reloadTime)
{
gunReloadTime = reloadTime;
}
/**
* Check whether there are any key pressed and react to them.
*/
private void checkKeys()
{
ignite(Greenfoot.isKeyDown("up"));
if(Greenfoot.isKeyDown("left")) {
setRotation(getRotation() - 5);
}
if(Greenfoot.isKeyDown("right")) {
setRotation(getRotation() + 5);
}
if(Greenfoot.isKeyDown("space")) {
fire();
}
}
/**
* Nyoom
*/
private void ignite(boolean boosterOn)
{
if(boosterOn) {
setImage("butterflyWithThrust.png");
acceleration.setDirection(getRotation());
increaseSpeed(acceleration);
}
else {
setImage(rocket);
}
}
/**
* Fire a bullet if the gun is ready.
*/
private void fire()
{
if(reloadDelayCount >= gunReloadTime) {
Bullet b = new Bullet(getMovement().copy(), getRotation());
getWorld().addObject(b, getX(), getY());
b.move();
shotsFired++;
reloadDelayCount = 0;
}
}
private void handleCollisions()
{
Actor enemy = getOneIntersectingObject(Bubble.class);
if( enemy != null )
{
Forest w = (Forest) getWorld();
w.subtractHealth(25);
collisionCountdown = 20;
}
Actor healing = getOneIntersectingObject(Orb.class);
if ( healing != null )
{
Forest w = (Forest) getWorld();
w.addHealth(10);
collisionCountdown = 15;
}
}
}
