From ef87700ace4e8b1a7de660d652c05d70d01fa567 Mon Sep 17 00:00:00 2001 From: oakes Date: Fri, 23 May 2014 13:50:34 -0400 Subject: [PATCH] Use input->screen in tutorial --- TUTORIAL.md | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/TUTORIAL.md b/TUTORIAL.md index 580df8e..1761c1c 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -111,20 +111,21 @@ Now, what about mobile devices? We may not have a keyboard, so let's create an ` ) ``` -In this case, the screen map will contain an `:input-x` and `:input-y` for the point on the screen that was touched. We can simply check to see what part of the screen this point is by using `game` to get the overall game's width and height. +In this case, the screen map will contain an `:input-x` and `:input-y` for the point on the screen that was touched. These are input coordinates, so normally it is best to convert them into screen coordinates with [input->screen](http://oakes.github.io/play-clj/core.input_screen.html) so they're using the same coordinate system as the entities. ```clojure :on-touch-down (fn [screen entities] - (cond - (> (:input-y screen) (* (game :height) (/ 2 3))) - (println "down") - (< (:input-y screen) (/ (game :height) 3)) - (println "up") - (> (:input-x screen) (* (game :width) (/ 2 3))) - (println "right") - (< (:input-x screen) (/ (game :width) 3)) - (println "left"))) + (let [pos (input->screen screen (:input-x screen) (:input-y screen))] + (cond + (> (:y pos) (* (game :height) (/ 2 3))) + (println "up") + (< (:y pos) (/ (game :height) 3)) + (println "down") + (> (:x pos) (* (game :width) (/ 2 3))) + (println "right") + (< (:x pos) (/ (game :width) 3)) + (println "left")))) ``` Conveniently, the `:on-touch-down` function also runs when a mouse is clicked on the screen, so we are adding mouse support to the game as well! @@ -160,15 +161,16 @@ Now we can update our `:on-key-down` and `:on-touch-down` functions to move the (move (first entities) :left))) :on-touch-down (fn [screen entities] - (cond - (> (:input-y screen) (* (game :height) (/ 2 3))) - (move (first entities) :down) - (< (:input-y screen) (/ (game :height) 3)) - (move (first entities) :up) - (> (:input-x screen) (* (game :width) (/ 2 3))) - (move (first entities) :right) - (< (:input-x screen) (/ (game :width) 3)) - (move (first entities) :left))) + (let [pos (input->screen screen (:input-x screen) (:input-y screen))] + (cond + (> (:y pos) (* (game :height) (/ 2 3))) + (move (first entities) :up) + (< (:y pos) (/ (game :height) 3)) + (move (first entities) :down) + (> (:x pos) (* (game :width) (/ 2 3))) + (move (first entities) :right) + (< (:x pos) (/ (game :width) 3)) + (move (first entities) :left)))) ``` ## Camera