(Presented “as-is” and without warranty or implied fitness; use at your own risk.)
First, the timer code:
type Animator () =
let timer =
new System.Windows.Threading.DispatcherTimer()
let stop () =
if timer.IsEnabled then timer.Stop()
let animate (fCallback:unit->bool)
e =
if not(fCallback()) then stop()
member this.Start (fCallback:unit->bool)
(interval:int) =
stop()
timer.Tick.Add (animate fCallback)
timer.Interval <- new System.TimeSpan(0,0,0,0,interval)
timer.Start()
member this.Stop () =
stop()
And a simple use that makes random moves (to be placed within MainWindow or whatever class displays the Fifteen puzzle):
let Animate =
let animator = new Animator()
let rand = new Random()
(fun interval ->
animator.Start
(fun () ->
moveTile(rand.Next 4)
true)
interval)
No comments:
Post a Comment