1

i have to create a traffic simulator, i want to move a car (picturebox) while the traffic light is not red and have it stopped when it is red, at the point of the traffic light. The traffic lights are 3 pannels changing color.

My problem is that my car does not stop when it meets the red traffic light. I have tried the "pictureBox.Location.X" and the "pictureBox.Left" but it just doesn't work, the pictureBox just goes on moving. I have also tried throwing a messageBox when the car meets the traffic light just to see if the "pictureBox.Location.X" is working but still nothing. Seems like it does not recognize the function.

I have tried to make the traffic light both with panels and pictureboxes but still the same.

Bellow i send you my code so far:

    private void timerCar1_Tick(object sender, EventArgs e)
            {
                //timerCar2.Enabled = true;

                if(panelRed.BackColor == Color.Red)
                {
                    car1.Left -= 5;
                }                
 /* ---> */     else if (car1.Location.X == panelGreen.Location.X & panelRed.BackColor == Color.Red)
                {
                    car1.Left -= 0;
                }
                else if (panelGreen.BackColor == Color.Green)
                {
                    car1.Left -= 20;
                }
                else if (panelOrange.BackColor == Color.Orange)
                {
                    car1.Left -= 10;
                }

            }
Avra
  • 13
  • 3
  • You seem to have posted more code than what would be reasonable for your issue. Please read [ask] and how to make a [mcve], providing a MCVE helps users answer your question and future users relate to your issue. – rene Dec 21 '17 at 21:46
  • Time to learn to debug your code. Set breakpoints, watch variables, execute code one line at a time, examine expected code branching to actual. Fix as needed. Its also past time to read [ask] and take the [tour]. You do not need 3 timers to run one traffic light. – Ňɏssa Pøngjǣrdenlarp Dec 21 '17 at 21:46
  • It seems the algorithm for moving and stopping a vehicle is in the `timerCar1_Tick` method. Therefore, you can remove all the other code from your question and explain: **1)** what you are trying to do in that code **2)** Explain what `panelRed` etc is. Then we will be able to help you. Right now, I cannot figure out what you are doing in that code. Plus the rest of the code is just noise. – CodingYoshi Dec 21 '17 at 22:08
  • Sorry guys for the noise, it is the first time i use Staack Overflow, i hope i made it more clear now. I have three panels one over another that work as the traffic light, panelRed is the traffic light when it is red, they are all three of them aligned vertically. So when the panelRed is Red and the car is under the panels, i want to (the car) stop. – Avra Dec 21 '17 at 22:30

2 Answers2

0

You use &.

 else if (car1.Location.X == panelGreen.Location.X & panelRed.BackColor == Color.Red)

I've heard this can lead to unintended behavior. Did you mean to use the logical "AND" (&&)?

 else if (car1.Location.X == panelGreen.Location.X && panelRed.BackColor == Color.Red)
B3W
  • 166
  • 11
0

I think that in c# for the logical "AND" ypu can use both & and &&. The problem was fixed by changing the pixel move from += 20 to += 3, so i think my picturebox would not meet the point i set as stopping point, due to the fact that it was moving 20 20 pixels.

Avra
  • 13
  • 3