This site requires JavaScript, please enable it in your browser!
Greenfoot back
Samas
Samas wrote ...

2016/10/11

Missile in a certain direction

Samas Samas

2016/10/11

#
I am trying to get a projectile, ShipMissile, to go in a certain direction based on the direction the ship is facing.
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);
            } 
    }    
}
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?
Samas Samas

2016/10/11

#
never mind I figured it out
You need to login to post a reply.