0
\$\begingroup\$

Think of a traffic light that only turns green or red.

enter image description here

If it is green, the next car passing through the detection point (pink square) will be directed to the green path and will cause the traffic light to turn red.

enter image description here

This way the next car that passes, will be directed to the red path, also making the green light, and so on.

Let's assume that two cars are in the same place and at the same speed and thus collide with the pink square at the same time. How can we make both directions to the same path be green or red and so that the light of the traffic light changes only once?

enter image description here

Considering "obj_BolaAzul" as "obj_blueCar", "obj_BolaRoxa" as "obj_purpleCar" and "obj_Cannon" as "obj_trafficLight".

enter image description here

The codes are basically:

obj_traficLight collision with obj_blueCar

if greenLight==true{
  obj_blueCar.move_towards_point(x+1,y,manSpeed); //go to right
  testGreen=true;
}

if redLight==true{
  obj_blueCar.move_towards_point(x-1,y,manSpeed); //go to left
  testRed=true;
}

if testGreen==true{
  greenLight=false;
  redLight=true
}

if testRed==false{
  redLight=false;
  greenLight=true
}

There are more things besides in code, but it has no relation to the problem.

I'm sorry again for the translation errors, I'm still not speaking English.

\$\endgroup\$
  • \$\begingroup\$ I'm not sure if that's available on GMS1, but have you taken a look at scripts? Scripts allows you to use the same piece of code for multiple objects, it's like how methods works in C# \$\endgroup\$ – Steven Jul 30 '18 at 6:07
0
\$\begingroup\$

The solution is to add a value to a variable x after the collision of any of the cars, so that it will only modify the traffic light after all cars have passed the traffic light.

Then you makes equal the value of the variable that modifies the semaphore y to that of the variable x in the step event.

In code:

collision event:

if y==10{
  car to red way;
  x=20;
}

if y==20{
  car to green way;
  x=10;
}

----------------------------------------------------------------------------------------------
step event:

if x==10{
  y=10;
}

if x==20{
  y=20;
}

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.