From 96016000517f180ed35d649316f1946951b809d7 Mon Sep 17 00:00:00 2001 From: oakes Date: Tue, 20 May 2014 18:16:17 -0400 Subject: [PATCH] If a timer already exists with a given id, stop and replace it with the new one --- src/play_clj/core_utils.clj | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/play_clj/core_utils.clj b/src/play_clj/core_utils.clj index c517f81..b0d8645 100644 --- a/src/play_clj/core_utils.clj +++ b/src/play_clj/core_utils.clj @@ -100,13 +100,17 @@ specified path. (defn ^:private create-and-add-timer! [{:keys [update-fn!] :as screen} id] - (when-let [timer (timer*)] + (some-> (get-in screen [:timers id]) .stop) + (let [timer (timer*)] (update-fn! assoc-in [[:timers id] timer]) timer)) (defn add-timer! "Returns a [Timer](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Timer.html) -that runs the :on-timer function according to the given arguments. +which runs which will fire the :on-timer function one or more times (depending +on the given arguments). In the :on-timer function, the id will be passed in the +screen map. If a timer with that id already exists in the screen, it will be +stopped and replaced with a new timer. ; wait 2 seconds and run once (add-timer! screen :spawn-enemy 2)