2
$\begingroup$

I have a Manipulate function with 2d slider which I can play with some curves. I consider this as a game (gamification in physics). I want to set a timer so when it countdown to zero then the game stops and I cannot change the slider. or something like this.

$\endgroup$
2
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, [by clicking the checkmark sign](tinyurl.com/4srwe26 $\endgroup$
    – Dunlop
    Commented Mar 3, 2020 at 19:20
  • $\begingroup$ Perhaps you can show an example code of what you can try? This will help others in providing solutions to your question. $\endgroup$
    – Dunlop
    Commented Mar 3, 2020 at 19:21

2 Answers 2

5
$\begingroup$

Update: Using Trigger to trigger/pause/reset the timer:

DynamicModule[{time, lim = 10.}, 
 Manipulate[ParametricPlot[{Sin[θ + d[[1]]], Sin[θ + d[[2]]]}, {θ, 0, 2 Pi}, 
     PlotStyle -> color, ImageSize -> Small],
  Row[{Dynamic @ PaddedForm[Round[1. - time, .001], {3, 3}], Spacer[5], 
    ProgressIndicator[Dynamic@(1 - time), {0, 1}], 
    Control[{{time, 0, ""}, 0, 1, Trigger[##, DefaultDuration -> lim] &}]}],
  {color, Red, Enabled -> Dynamic[time < 1]}, 
  {{d, {Pi/2, Pi/3}, "d"}, {0, 0}, {Pi, Pi}, Appearance -> "Labeled", 
     Enabled -> Dynamic[time < 1]}, 
  TrackedSymbols :> {d, color, time}]]

enter image description here

Original answer:

DynamicModule[{t, lim = 10}, 
 Manipulate[ ParametricPlot[{Sin[θ + d[[1]]], Sin[θ + d[[2]]]}, {θ, 0, 2 Pi}, 
   PlotStyle -> color , ImageSize -> Small],
  Dynamic[Row@{"  time  ", ProgressIndicator[ 1 - (t = Clock[{0, 1}, lim, 1])]}],
  {color, Red, Enabled -> Dynamic[t < 1]}, 
  {{d, {Pi/2, Pi/3}, "d"}, {0, 0}, {Pi, Pi},
   Appearance -> "Labeled", Enabled -> Dynamic[t < 1]}, 
  TrackedSymbols :> {d, color}]]

enter image description here

$\endgroup$
12
  • $\begingroup$ Nice use of Enabled $\endgroup$
    – b3m2a1
    Commented Mar 3, 2020 at 21:37
  • $\begingroup$ I like this solution but noticed something that may need to be addressed: if you hold down the button for "d" and keep it held down, then it doesn't "respect" the timer. In other words, even after the timer has expired, you may continue manipulating the graph. $\endgroup$
    – Mark R
    Commented Mar 4, 2020 at 2:56
  • $\begingroup$ @MarkR, excellent observation. I cannot think of a fix right now. $\endgroup$
    – kglr
    Commented Mar 4, 2020 at 4:23
  • $\begingroup$ @MarkR are you a play-tester? Haha! Do you find it works(or doesn’t work) for any other key being held? $\endgroup$ Commented Mar 4, 2020 at 19:12
  • 1
    $\begingroup$ @Aaron, unfortunately I don't know how to fix that issue. I suggest you update your question with the requirement that controls are disabled when time runs out even when the mouse is kept down continuously. $\endgroup$
    – kglr
    Commented Mar 18, 2020 at 23:10
4
$\begingroup$

I think this is what you're asking for?

timeLimitedManipulator~SetAttributes~HoldAllComplete
timeLimitedManipulator[var_, dom_, {timer_, lim_}, ops : OptionsPattern[]] :=
  Manipulator[
   Dynamic[var, If[! TrueQ[timer >= lim], Set[var, #]] &],
   dom, 
   ops
   ];
DynamicModule[
 {t, limit = 3},
 DynamicWrapper[
  Manipulate[
   Plot[Sin[n*π*x], {x, 0, 2 π}],
   {{n, 1}, timeLimitedManipulator[n, {.1, 5}, {t, limit}] &}
   ],
  t = Clock[{0, limit}, limit, 1]
  ]
 ]

Basically I wrap a timer around my Manipulate and force the variable n to be updated by a Manipulator that listens to this timer

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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