# Using IF commands in a Dynamic Module

Is there a way to use a functional If command in a DynamicModule? I have a code which runs a car driving up a street using Clock to control its position on the y-axis and the PlotRange. I have MousePosition controlling the position of the car on the x-axis? I have tried multiple ways to input an If command so that if the car runs into an obstacle, a CreateDialog box will pop up saying you have crashed. I cannot figure out how to make this If command cooperate with the dynamic functionality. Any help?

Clarification: Here is a small example of the code that has the heart of the problem. The "car" variable is undefined, but can be easily replaced with just a point, using the x and y variables as its coordinates.

finalS = {Slider[Dynamic[v], {10, 500, 10}], Dynamic[v]}; final =
DynamicModule[{}, {Graphics[{{White, Thick,
Line[{{200, -500}, {200, 175000}}]}, {Yellow, Thick,
Dashing[{.1, .05}], Line[{{500, -500}, {500, 175000}}]}, {White,
Thick, Line[{{800, -500}, {800, 175000}}]},
Inset[Graphics[{Orange, Disk[{0, 0}, {3, 4}]}], {x, y}]},
Axes -> False,
PlotRange -> {{0, 1000}, {-250 + (z Dynamic[v]),
750 + (z Dynamic[v])}}, Background -> Black, ImageSize -> 825],
z = Dynamic[Clock[300]];, x = Dynamic[MousePosition[][[1]]];,
y = Dynamic[z*v];}];
finalS final

• Yes, it is possible and I used in If inside this answer. To get more specific help from anybody you should add a minimal example to your question. Commented May 14, 2016 at 14:01
• Say I wanted to use an if command so that would execute if the car drove off of the road line, how would that be possible? Commented May 14, 2016 at 14:17
• Ishnor -- I reformatted your code so that it can be copied. I changed "car" to an orange ellipse. But I do not know what kind of answer you are looking for: what kind of collision with what obstacle? Commented May 14, 2016 at 16:05

## 1 Answer

This will open a dialog notebook created by CreateDialog as soon as you "drive" over the left sideline.

final = DynamicModule[{dialogQ = False},
{Graphics[{{White, Thick, Line[{{200, -500}, {200, 175000}}]},
{Yellow, Thick, Dashing[{.1, .05}], Line[{{500, -500}, {500, 175000}}]},
{White, Thick, Line[{{800, -500}, {800, 175000}}]},
Inset[Graphics[{Orange, Disk[{0, 0}, {3, 4}]}], {x, y}]},
Axes -> False, PlotRange -> {{0, 1000}, {-250 + (z Dynamic[v]),
750 + (z Dynamic[v])}}, Background -> Black,
ImageSize -> 825], z = Dynamic[Clock[300]];,
x = Dynamic[MousePosition[][[1]]], y = Dynamic[z*v];
DynamicWrapper[Dynamic[dialogQ, TrackedSymbols :> {dialogQ}],
If[Setting@x < 200 && ! dialogQ, dialogQ = True;
CreateDialog[{TextCell["You went " Style["OFF ROAD!", Red]],
DefaultButton[DialogReturn[dialogQ = False]]}, Modal -> True]],
TrackedSymbols :> {x}]}];


I just added the part containing the If statement at the end of your code leaving the rest unchanged.

• awesome thank you so much. I should be able to do the rest of the If commands based off of this. Much appreciated Commented May 14, 2016 at 20:51