I am trying to get a projectile, ShipMissile, to go in a certain direction based on the direction the ship is facing.That is the code for the missile itself. I have code in my Ship class called Bug that adds or subtracts 3 to a variable called angle that covers 0-360 degrees by threes( the angle which the Ship turns when left or right is held down). My main problem is that the setRotation() code doesn't seem to work or that the value of angle is not carrying over from the Ship Actor to the ShipMissile Actor. Can anyone help?
import greenfoot.*;
public class ShipMissile extends Bug
{
public int direction;
public ShipMissile(int dir)
{
direction = angle;
}
public void act()
{
int dist = 1000;
Actor closest = null;
Actor MeanShip = null;
if (!getWorld().getObjects(BadGuys.class).isEmpty())
{
MeanShip = (Actor)getWorld().getObjects(BadGuys.class).get(0);
}
if (MeanShip != null && !getObjectsInRange(dist, BadGuys.class).isEmpty())
{
for (Object obj: getObjectsInRange(dist, BadGuys.class))
{
int MeanShipDist = (int) Math.hypot(MeanShip.getX() - getX(), MeanShip.getY() - getY());
if (closest == null || MeanShipDist< dist)
{
closest = MeanShip;
dist = MeanShipDist;
}
}
turnTowards(closest.getX() + 20 ,closest.getY() + 20);
move(8);
}
else
{
setRotation(direction);
move(8);
}
}
}