# Restarting an Animate animation after variable in Manipulate changes

I've been trying to restart an animation after variables change from Manipulate PopUpMenus. My animation takes the form:

Animate[expr, {u, umin, umax, du}].


I can change u, after variations to Manipulate, back to an initial value momentarily, but from there u jumps back to its previous position (or farther forward) as if its continued running. I would be content with simply restarting the entire animation after changing the Manipulate variables.

My code for Animate[] is in the following simplified form (I hope to make the i slider bar return back to the left and the red dot to begin being plotted at x=-10 each time the function is changed):

gPrev[x_] := Sin[x];
Manipulate[
Switch[functionNumb,
0, g[x_] := Sin[x],
1, g[x_] := Cos[x],
2, g[x_] := ArcTan[x]],
{functionNumb, 0, 2, 1}]

Animate[
Plot[{g[x]}, {x, -10, 10},
Epilog -> {Red, PointSize[0.015], Point@{i, g[i]}}], {i, -10, 10, 1},
DisplayAllSteps -> True, AnimationRunning -> False]

-
if you post a (formatted) minimal working example you will probably get more/better answers. –  Ajasja Jun 4 '13 at 14:42
Thank-you for your suggestion. –  twhoward99 Jun 4 '13 at 15:04
You are welcome, but the code snippet is not working. It is also not exactly minimal:) –  Ajasja Jun 4 '13 at 15:08
Simplified, working code posted, thanks again. –  twhoward99 Jun 4 '13 at 16:01
It's much better now (+1). Hmm I see where the problem is. Well, let me first give you an improved version of your original code. Manipulate[ Animate[Plot[{f[x]}, {x, -10, 10}, Epilog -> {Red, PointSize[0.015], Point@{i, f[i]}}], {i, -10, 10, 1}, DisplayAllSteps -> True, AnimationRunning -> False], {f, {Sin, Cos, ArcTan}}] –  Ajasja Jun 4 '13 at 16:18

Not a complete answer yet: I've built the functionality using DynamicModule and Animator, thinking that it will be possible to manually set the index i when using the second argument of Dynamic. Sadly this only works if the animation is not running. So if the animation is not running i is reset correctly. Still, here is the code

EDIT Stealing from Kuba, I'm 80% there. The animation is restarted, but the old animation state is lost. In other words, changing the function will always run the animation.

DynamicModule[{i = -10, f = Sin, run = False},
Column@{SetterBar[
Dynamic[f, (i = -10; run = False; FinishDynamic[]; run = True;
Print@i; f = #) &], {Sin, Cos, ArcTan}],
Dynamic@Animator[Dynamic[i], {-10, 10, 0.1},
ContinuousAction -> True, AnimationRunning -> run],
Dynamic@Plot[{f[x]}, {x, -10, 10}, ImageSize -> Medium,
Epilog -> {Red, PointSize[0.015], Point@{i, f[i]}}]}]

-
Hmm, there is an "ResetButton" under AppearanceElements, but I don't know how to call it from code. –  Ajasja Jun 4 '13 at 16:51
Ahh, You are faster :) I think it works like twhoward99 wanted. +1 –  Kuba Jun 4 '13 at 17:08
I can't upvote due to my meager reputation, but thank-you so much for your help! –  twhoward99 Jun 4 '13 at 17:18

First I've give one way that fits with the setup in your question, and then I'll give the way I would do it myself, in case that helps.

Wrap Animate in Dynamic and make it track g; also keep the Manipulate from tracking g. Wrapping Animate will make it reset whenever g is changed.

gPrev[x_] := Sin[x];
Manipulate[
Switch[functionNumb, 0, g[x_] := Sin[x], 1, g[x_] := Cos[x], 2,
g[x_] := ArcTan[x]]; g[x], {functionNumb, 0, 2, 1},
TrackedSymbols :> {functionNumb}]

Dynamic@Animate[
Plot[{g[x]}, {x, -10, 10},
Epilog -> {Red, PointSize[0.015],
Dynamic@Point@N@{i, g[i]}}], {i, -10, 10, 1},
DisplayAllSteps -> True, AnimationRunning -> False,
TrackedSymbols -> {g}]


Here's a single Manipulate that has the same functionality, as I see it, and a more integrated look.

Manipulate[
Column[{
i = -10;
Animator[Dynamic[i], {-10, 10, 1}, AnimationRunning -> False],
Plot[function, {x, -10, 10},
Epilog -> {Red, PointSize[0.015], Dynamic@Point@N@{i, function /. x -> i}},
ImageSize -> Medium]
}],
{function, {Sin[x], Cos[x], 2 ArcTan[x]}},
{i, -10, 10, 1, ControlType -> None},
TrackedSymbols :> {function}]


Putting the Animator in the control area is a bit trickier, it seems. It can be done, but my solution is a bit too kludgy to post.

-