2
$\begingroup$

Recently I was trying to make something where one could vary the parameters and press play to see how the animation changed. However, when I tried running the code it would just start automatically. Even if I pressed pause nothing would happen. I managed to narrow the problem down and have made a simple example to illustrate the issue.

If I run the following code I get what I want. (i.e. press play and it starts. Press pause and it stops)

Clear[step,MyAnimation,dynamicVar]
step=0;
dynamicVar={1,2};
MyAnimation[nothing_,something_]:=Module[{},step++;something+{step,step}]
Manipulate[
    dynamicVar=MyAnimation[time,{1,2}];
    Dynamic@dynamicVar,
    {{time,1,""},1,\[Infinity],1,ControlType->Trigger,AnimationRate->2}
]

However, if I replace dynamicVar=MyAnimation[time,{1,2}]; with dynamicVar=MyAnimation[time,dynamicVar]; what I get is something which runs automatically and which one cannot not stop. (I know they give two different results, but the issue lies in the fact that I cannot stop it)

Clear[step,MyAnimation,dynamicVar]
step=0;
dynamicVar={1,2};
MyAnimation[nothing_,something_]:=Module[{},step++;something+{step,step}]
Manipulate[
    dynamicVar=MyAnimation[time,dynamicVar];
    Dynamic@dynamicVar,
    {{time,1,""},1,\[Infinity],1,ControlType->Trigger,AnimationRate->2}
]

Any advice on how to fix this problem?

$\endgroup$
4
  • $\begingroup$ You're creating an infinite recursion with dynamicVar = MyAnimation[time, dynamicVar] -- what are you really trying to do? $\endgroup$
    – Mr.Wizard
    Commented Sep 14, 2012 at 10:30
  • $\begingroup$ @Mr.Wizard Every second I want MyAnimation[time, dynamicVar] to evaluate and assign the result to dynamicVar. $\endgroup$
    – E.O.
    Commented Sep 14, 2012 at 10:35
  • 1
    $\begingroup$ I'm glad you found my answer helpful, but if you wait longer (IMHO a day is good) to Accept an answer, you might get something more helpful. $\endgroup$
    – Mr.Wizard
    Commented Sep 14, 2012 at 10:53
  • $\begingroup$ @Mr.Wizard OK, I'll keep that in mind next time:) $\endgroup$
    – E.O.
    Commented Sep 14, 2012 at 12:00

1 Answer 1

2
$\begingroup$

Based on your comment:

Every second I want MyAnimation[time, dynamicVar] to evaluate and assign the result to dynamicVar

I believe you want something like this:

Manipulate[
  Dynamic @ Refresh[dynamicVar = MyAnimation[time, dynamicVar], TrackedSymbols -> {time}],
  {{time, 1, ""}, 1, ∞, 1, ControlType -> Trigger, AnimationRate -> 1}
]

Key elements are Refresh and TrackedSymbols.


After a bit of reading it seems Refresh is extraneous here as one can use:

Manipulate[
  Dynamic[dynamicVar = MyAnimation[time, dynamicVar], TrackedSymbols -> {time}],
  {{time, 1, ""}, 1, \[Infinity], 1, ControlType -> Trigger, AnimationRate -> 1}
]

It might still be useful in your full application.

$\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.