This question came about because of my failure to implement an answer to this previous question. My idea was that an answer could be written using aClickPane
and anAnimator
.
Code
points[goal : {_, _}, nPts_Integer] :=
Table[i goal, {i, 0, 1, 1/(nPts - 1)}]
With[{wayPts = 5},
DynamicModule[{pts = points[{40, 40}, wayPts], k, t},
k = t = Length @ pts;
Column[{
Animator[
Dynamic @ k, {1, Dynamic @ Length@pts, 1}, 1,
AppearanceElements -> "ResetPlayButton",
AnimationRepetitions -> 1,
AnimationRunning -> False,
AnimationRunTime -> Dynamic @ t],
Dynamic @
ClickPane[
Graphics[{Red, Disk[pts[[k]], 3]},
PlotRange -> {{-50, 50}, {-50, 50}},
ImageSize -> Medium,
Frame -> True],
(Which[
k >= Length @ pts && t >= 0., t = 0.; k = 1; pts = points[#, wayPts],
k == 1 && t == 0., pts = points[#, wayPts]]&)],
(* debugging info *)
Dynamic @ t, Dynamic @ k}]]]
Instructions for use
Click in the pane to establish a target point; the dot will move to the origin.
Click on the
ResetPlay
button (top-left); the dot will move along a straight line to the target point in discrete steps.
The problem
The code misbehaves. It does everything that it is supposed to do, but it also does something that is totally unexpected and unwanted – after moving the dot to the target point, it immediately returns the dot to the origin and moves it back to the target point. This second iteration of the moves is strange in that only indexes 1, 2, and 5 appear, 3 and 4 are skipped.
I have not been able to determine why the animator makes a second iteration or how to fix it. I have little experience with animators. This is first time I have tried to control one dynamically.
Questions
Is this misbehavior version and/or platform dependent? I'm running V10.0.2 on OS X 10.6.8 (Snow Leopard).
Is there a better strategy for controlling the animator, so that it runs only one iteration of the index values and then stops?
Update
Inspired by beliarius' comment, I investigated this problem in V9. I found that the option AnimationRunTime -> Dynamic @ t
is not support there. I removed it and all references to variable t
. The code then ran with no sign of any misbehavior. I then tried the modified code in V.10.0.2 and the misbehavior still occurred. This make me think I have run into a bug in V.10.0.2,
Here is the modified code for anyone who wants to experiment with it.]
With[{wayPts = 5},
DynamicModule[{pts = points[{40, 40}, wayPts], k},
k = Length @ pts;
Column[{
Animator[
Dynamic @ k, {1, Dynamic @ Length @ pts, 1}, 1,
AppearanceElements -> "ResetPlayButton",
AnimationRepetitions -> 1,
AnimationRunning -> False],
Dynamic @
ClickPane[
Graphics[{Red, Disk[pts[[k]], 3]},
PlotRange -> {{-50, 50}, {-50, 50}},
ImageSize -> Medium,
Frame -> True],
(If[k >= Length @ pts, k = 1; pts = points[#, wayPts]] &)],
(* debugging info *)
Dynamic @ k}]]]