Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with implementing Phaser Tutorial #7

Closed
turtle-bazon opened this issue Dec 27, 2015 · 3 comments
Closed

Problems with implementing Phaser Tutorial #7

turtle-bazon opened this issue Dec 27, 2015 · 3 comments

Comments

@turtle-bazon
Copy link

@turtle-bazon turtle-bazon commented Dec 27, 2015

I try to implement phaser tutorial in phzr Making your first phaser game, but has several problems. When I do ground (pgr/create @*platforms* 0 (- game-world-h 64) "ground"), and then (pset! (-> ground :body) :immovable true) problem that body null. Also after
(defn p-update [game] (-> game :physics :arcade (ppa/collide @*player* @*platforms*)))
Collides not works.

@turtle-bazon
Copy link
Author

@turtle-bazon turtle-bazon commented Dec 27, 2015

Whole code.

(ns phaser-tutorial.core
  (:require
   [phzr.animation-manager :as pam]
   [phzr.core :as p :refer [pset!]]
   [phzr.game :as pg]
   [phzr.game-object-factory :as pgof]
   [phzr.group :as pgr]
   [phzr.loader :as pl]
   [phzr.physics :as pp]
   [phzr.physics.arcade :as ppa]
   [phzr.point :as ppnt]))

(def *platforms* (atom nil))

(def *player* (atom nil))

(defn p-preload [game]
  (let [loader (:load game)]
    (doto loader
      (pl/image "sky" "assets/sky.png")
      (pl/image "ground" "assets/platform.png")
      (pl/image "star" "assets/star.png")
      (pl/spritesheet "dude" "assets/dude.png" 32 48))))

(defn p-create [game]
  (let [pgof (:add game)
        physics (:physics game)
        game-world-h (-> game :world :height)]
    (pp/start-system physics (pp/const :arcade))
    (pgof/sprite pgof 0 0 "sky")
    (reset! *platforms* (-> game :add (pgof/group "platforms" false true)))
    (reset! *player* (pgof/sprite pgof 32 (- game-world-h 150) "dude"))
    (let [ground (pgr/create @*platforms* 0 (- game-world-h 64) "ground")
          ledge1 (pgr/create @*platforms* 400 400 "ground")
          ledge2 (pgr/create @*platforms* -150 250 "ground")
          player @*player*]
      (-> ground :scale (ppnt/set-to 2 2))
      ;(pset! (-> ground :body) :immovable true)
      ;(pset! (-> ledge1 :body) :immovable true)
      ;(pset! (-> ledge2 :body) :immovable true)
      (-> physics :arcade (pp/enable player))
      (pset! (-> player :body :bounce) :y 0.2)
      (pset! (-> player :body :gravity) :y 300)
      (pset! (-> player :body) :collide-world-bounds true)
      (-> player :animations (pam/add "left" [0 1 2 3] 10 true))
      (-> player :animations (pam/add "right" [5 6 7 8] 10 true))
      )))

(defn p-update [game]
  (-> game :physics :arcade (ppa/collide @*player* @*platforms*)))

(defn start []
  (pg/->Game 800 600 (p/phaser-constants :auto) "game"
             {"preload" p-preload
              "create" p-create
              "update" p-update}))

(set! (.-onload js/window) start)
@turtle-bazon
Copy link
Author

@turtle-bazon turtle-bazon commented Dec 27, 2015

Interesting. But if I do via js interop directly, I have same result

(ns phaser-tutorial.core
  )

(def *platforms* (atom nil))

(defn preload-fn [game]
  (let [loader (.-load game)]
    (.image loader "sky" "assets/sky.png")
    (.image loader "ground" "assets/platform.png")
    (.image loader "star" "assets/star.png")
    (.spritesheet loader "dude" "assets/dude.png" 32 48)))

(defn create-fn [game]
  (let [add (.-add game)]
    (.startSystem (.-physics game) js/Phaser.Physics.ARCADE)
    (.sprite add 0 0 "sky")
    (reset! *platforms* (.group add))
    (set! (.-enableBody *platforms*) true)
    (let [world-h (.-height (.-world game))
          ground (.create @*platforms* 0 (- world-h 64) "ground")
          ledge1 (.create @*platforms* 400 400 "ground")
          ledge2 (.create @*platforms* -150 250 "ground")]
      (.setTo (.-scale ground) 2 2)
      (set! (.-immovable (.-body ground)) true)
      ;(set! (.-immovable (.-body ledge1)) true)
      ;(set! (.-immovable (.-body ledge2)) true)
      )))

(defn update-fn [game]
  )

(defn start []
  (js/Phaser.Game. 800 600 js/Phaser.AUTO "game"
                   (js-obj "preload" preload-fn
                           "create" create-fn
                           "update" update-fn)))

(set! (.-onload js/window) start)
@turtle-bazon
Copy link
Author

@turtle-bazon turtle-bazon commented Jan 19, 2016

In first example problem was in forgetting add physics body to group, in second example problem was in forgetting deref while trying to add property to platforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.