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

2015/3/28

can i use getOneIntersectingObject for a object of a class in world instead of the class itself?

Rayray Rayray

2015/3/28

#
i want to use a specfic object of the class and not the class itself?
Rayray Rayray

2015/3/28

#
for example i want to car to only turn left on a object of detector and right on another object of detector
danpost danpost

2015/3/28

#
If you are wanting to be able to distinguish between the two detectors, there are many ways of doing it. Just to list a few: (1) you could add a field to the detector class and give them different values that the car can then make a determination as to which way to go from its value. (2) you could use the location of the detector as an indication as to which one it is and only do one action at one location and a different one at the other. (3) you could rotate the detector so that the car can use its rotation to determine which way to go.
Rayray Rayray

2015/3/28

#
could you provide example code of these?
danpost danpost

2015/3/28

#
Rayray wrote...
could you provide example code of these?
Method 3 would probably be easiest. When you create and add the pads into the world, rotate them in the direction you want the car to turn when it gets to them. The cars may also need to keep track as to which pad they are currently at; and you may need to use a boolean to indicate whether the turn at that pad was made yet or not. It all depends on the size relationships between the intersection and pad and where the car will be turning in relation to them. Providing example code is quite impossible without going over pretty much all you have already (both codes and images). At any rate, simply put, without regard to exactly when the car should turn; but, just to get the direction to turn from the pad if the pad is rotation at the time it is added into the world (using 'Pad' as the class of the detector):
// instance fields used
private Actor onPad; // to track what pad car is on
private boolean turned; // to indicate whether turn was made at pad or not
// in act or method called by act
Actor pad = getOneIntersectingObject(Pad.class);
if (pad != onPad) // moving onto or off of a pad
{
    onPad = pad;
    turned = false;
}
if (onPad != null) // on a pad
{
    if (!turned && /** time to turn */)
    {
        turn(onPad.getRotation());
        turned = true;
    }
}
Hope this makes some sense.
Rayray Rayray

2015/3/28

#
thanks for helping out
You need to login to post a reply.