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

2012/11/11

Automated Turret Issue

GreenGoo GreenGoo

2012/11/11

#
Hi, I made an automated turret which fires at a vehicle which you control. But I had to make the co-ordinates of Vehicle static: public static int vehicleX; public static int vehicleY; vehicleX = getX(); vehicleY = getY(); and then in my Turret class write: public void aim() { turnTowards(Vehicle.vehicleX, Vehicle.vehicleY); } It works, but how can I fix this?
SPower SPower

2012/11/11

#
Well, to make them non static, you need an object which holds them. This is the new aim method for that:
public void aim()
{
    Actor vehicle = getWorld().getObjects(Vehicle.class).get(0);
    if (vehicle != null)
        turnTowards(vehicle.getX(), vehicle.getY());
}
which results in not needing the variables anymore. What this code does is it gets the first Vehicle object in the world an turns towards it's location. If you don't understand something, just ask ;) PS. Next time you insert code, click the code button below the text field :)
GreenGoo GreenGoo

2012/11/11

#
Thanks for the quick reply! It works :)
GreenGoo GreenGoo

2012/11/11

#
Hold on, it says 'incompatible types' to the getWorld().GetObjects(Vehicle.class).get(0);
danpost danpost

2012/11/11

#
The return needs to be cast to an Actor type. Use
Actor vehicle = (Actor) getWorld().getObjects(Vehicle.class).get(0);
As an Object type, which is the type returned, it cannot be set to a field which holds an Actor type.
You need to login to post a reply.