Use :point-x and :point-y in tutorial

This commit is contained in:
oakes
2014-08-27 15:22:08 -04:00
parent e02272932f
commit 479f028f88

View File

@@ -111,21 +111,20 @@ 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. 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. The [game](http://oakes.github.io/play-clj/core.game.html) function gives you convenient access to the window dimensions and the x/y position of the pointer:
```clojure ```clojure
:on-touch-down :on-touch-down
(fn [screen entities] (fn [screen entities]
(let [pos (input->screen screen (:input-x screen) (:input-y screen))] (cond
(cond (> (game :point-y) (* (game :height) (/ 2 3)))
(> (:y pos) (* (height screen) (/ 2 3))) (println "up")
(println "up") (< (game :point-y) (/ (game :height) 3))
(< (:y pos) (/ (height screen) 3)) (println "down")
(println "down") (> (game :point-x) (* (game :width) (/ 2 3)))
(> (:x pos) (* (width screen) (/ 2 3))) (println "right")
(println "right") (< (game :point-x) (/ (game :width) 3))
(< (:x pos) (/ (width screen) 3)) (println "left")))
(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! 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!
@@ -162,16 +161,15 @@ Now we can update our `:on-key-down` and `:on-touch-down` functions to move the
:on-touch-down :on-touch-down
(fn [screen entities] (fn [screen entities]
(let [pos (input->screen screen (:input-x screen) (:input-y screen))] (cond
(cond (> (game :point-y) (* (game :height) (/ 2 3)))
(> (:y pos) (* (height screen) (/ 2 3))) (println "up")
(move (first entities) :up) (< (game :point-y) (/ (game :height) 3))
(< (:y pos) (/ (height screen) 3)) (println "down")
(move (first entities) :down) (> (game :point-x) (* (game :width) (/ 2 3)))
(> (:x pos) (* (width screen) (/ 2 3))) (println "right")
(move (first entities) :right) (< (game :point-x) (/ (game :width) 3))
(< (:x pos) (/ (width screen) 3)) (println "left")))
(move (first entities) :left))))
``` ```
## Camera ## Camera