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

2012/12/30

getOneObjectAtOffset has some troubles

Dytanoth Dytanoth

2012/12/30

#
Hey, I've been trying to let a car drive down a road, for this it will check if there is a road around himself. The idea is: Click the car -> Click location. Now it should drive towards that point. The problem is I've got 3 cars. (3x Car.class). Now when I click car 2 and 3, it will not follow the road. When I select car 1 it will work as intended. For some reason it will use car 1 to run the getOneObjectAtOffset. The way I have used my Offset : Actor roadLeft=getOneObjectAtOffset(-1,0,road.class); So I guess it uses this as: For Car 1: Actor roadLeft=getOneObjectAtOffset(-1,0,road.class); For Car 2: Actor roadLeft=getOneObjectAtOffset(Car1.x-1,0,road.class); For Car 3: Actor roadLeft=getOneObjectAtOffset(Car1.x-1,0,road.class); My question: I have written: Actor roadLeft=getOneObjectAtOffset(-1,0,road.class); Why does it read it for car 2 and 3 as: Actor roadLeft=getOneObjectAtOffset(Car1.x-1,0,road.class); And how can i fix it? Thanks
danpost danpost

2012/12/30

#
This could be a result of how you created the cars in the world; or a result of how you coded the Car class. I would need to see at least a trace-back to an act method and what class it is in (start with the 'getOneObjectAtOffset' statement and list the method it occurs in; then list the method that calls that method, and repeat until you are at an 'act' method; then inform what class each of those methods were in. Also needed are what pertinent variables you have in those classes listed with how they are declared. If there is nothing found there, then how they were created may have to be looked at (which would require more information).
Dytanoth Dytanoth

2012/12/30

#
Ok, so I have posted it http://www.greenfoot.org/scenarios/7149... I hope this will help
danpost danpost

2012/12/31

#
There are two ways that I can think of that will allow the action you require: (1) use a static variable in the 'CC_Voertuig_Politie' class to hold the auto last clicked on (or null if no auto clicked on); or (2) place a non-static variable in the world class for the same purpose as in (1) above. This variable will henceforth be referred to as 'the variable'. Using either method, when a vehicle is clicked on, set 'the variable' to that vehicle; when a location is clicked on and if 'the variable' is not null, set a non-static boolean field for the vehicle to true and two non-static int fields to the x and y location where the click took place. The 'act' method in this class can then use those non-static fields to determine if it is moving and where it is moving to. Once, the location has been arrived at (using a check in the same act method), reset the boolean field to false. Using the above procedure, you should be able to control any number of vehicles by clicking on the vehicle, followed by clicking at a location for it to move. Clicking at another location before clicking another vehicle should change the destination of the last clicked vehicle.
danpost danpost

2012/12/31

#
Unfortunately, there appears to be much more that needs worked out besides the above. You may have to remove and replace code (or classes) to narrow down where the problems are.
Dytanoth Dytanoth

2012/12/31

#
Thanks for the advice, I hope it'll fix the problem
You need to login to post a reply.