I've been having some difficulty with the aforementioned use of these three functions/options and would greatly appreciate some help.
I know that they're is probably an easy way of making a disk move across a square, but, as you may be able to guess, this is an abstraction of my real problem and thus I have to work with an object-oriented system (i.e., currying).
Let's say I wanted to make a disk move from the bottom left of a square to the top right, with enter as my play/pause control:
My code:
makeobject[name_String] := object[name]
(* Default values for position and velocity *)
object[name_]["pos"] = {0, 0};
object[name_]["vel"] = {.05, .05};
(* Return a graphic method *)
(this : object[name_])["pic"] := {EdgeForm[Pink], Blue,
Disk[this["pos"], .2]}
(* update *)
tick[obj_] := ((obj["pos"] = obj["pos"] + obj["vel"] (.1)); obj)
(* Dynamic pictures *)
DynamicModule[{play = False, myobj = makeobject["myobj"]},
EventHandler[
Dynamic[
EventHandler[
If[play,
myobj = tick[myobj];
Graphics[{Black, Rectangle[{0, 0}, {50, 50}], myobj["pic"]},
PlotRange -> {{0, 1}, {0, 1}}],
Graphics[{Black, Rectangle[{0, 0}, {50, 50}], myobj["pic"]},
PlotRange -> {{0, 1}, {0, 1}}]
],
{"ReturnKeyDown" :> (play = False)}],
UpdateInterval -> .01],
{"ReturnKeyDown" :> (play = True)}
],
Initialization :> ( myobj["pos"] = {0, 0})
]
The site's not letting me put up any pictures of what's happening, but when I evaluate, you get an image of black rectangle with the quarter circle at the bottom. Pressing enter makes the disk move up and right.
But, while it's updating, if I press enter, nothing happens, and the disk keeps moving. This means that it's still calling tick[myobj]
and therefore the "ReturnKeyDown":>(play=False)
never worked.
Thanks in advance for any help.
Sorry if I am missing any kind of information, this is my first question. Please let me know if so.
MMA 8.0.1.0 for students
OS Windows 7 64-bit
play = False
andplay = True
. What play will be will depend on what Eventhandler get executed last. See Rojo's answer below on how to code this. $\endgroup$PassEventsDown->False
. Would that be acceptable with nestedEventHandler
s? $\endgroup$