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

2012/4/16

im trying to make a driving game where you walk a guy to a car and then he disapears and you can drive the car

rcompton5 rcompton5

2012/4/16

#
im trying to make a driving game where you walk a guy to a car and then he disapears and you can drive the car. i then tried 01.public class Car extends Actor 02.{ 03. 04. private Guy driver = null; 05. 06. // More stuff here.... 07. 08.} and im only able to move the car when the man is touching the car
erdelf erdelf

2012/4/16

#
I think that you should use a boolean which will be activated when the guy touches the car. Put something like the following in the car class:
private boolean canDrive = false;
public void touches()
{
    Actor guy = getOneIntersectingObject(clss);
    return guy != null;
}
And the following in the act class:
if(touches(Guy.class))
{
    canDrive = true;
}
if(canDrive)
{
   otherThingsInActClass
}
danpost danpost

2012/4/16

#
@erdelf, the boolean is no neccessary as 'if (driver != null)' will do the trick. @rcompton5, you were on the right path. I would suggest a check in the Car act() method :
if (driver == null && !getOneIntersectingObject(Guy.class).isEmpty()) 
{
    driver = (Guy) (getOneIntersectingObect(Guy.class).get(0));
    getWorld().removeObject(driver);
}
later, when the trigger to reliquish the driver occurs, you can do
getWorld().addObject(driver, getX(), getY());
and have another check to clear the driver
if (driver != null && !getWorld().getObjects(Guy.class).isEmpty() && getOneIntersectingObject(Guy.class).isEmpty()) driver = null;
Also, you can have
if (driver != null && getWorld().getObjects(Guy.class).isEmpty())
{
    // car controls
}
You need to login to post a reply.