我正在尝试将一些简单的示例从JavaScript转换为ClojureScript,以熟悉ClojureScript,但是对于它来说,我无法绕过这个snippet:
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'example', { create: create });
function create() {
var text = "- phaser -\n with a sprinkle of \n pixi dust.";
var style = { font: "65px Arial", fill: "#ff0044", align: "center" };
var t = game.add.text(game.world.centerX-300, 0, text, style);
}
为:
;; Uncaught ReferenceError: game is not defined
(defn create []
(let [text "- phaser -\n with a sprinkle of \n pixi dust."
style (js-obj "font" "65px Arial" "fill" "#ff0044" "align" "center")]
(game.add.text (- game.world.centerX 300) 0 text style)))
(def game (js/Phaser.Game. 800 600 js/Phaser.CANVAS "example" #js {:create create}))
问题在于我还试图从ClojureScript中的函数自我引用待定义的对象 - 我感觉这在ClojureScript中是不对的和/或惯用的思考,所以如何有人接近这种情况吗?
编辑:
;; Working example
(declare game)
(defn create []
(let [text "- phaser -\n with a sprinkle of \n pixi dust."
style (js-obj "font" "65px Arial" "fill" "#ff0044" "align" "center")]
(.text game.add (- (.-centerX game.world) 300) 0 text style)))
(def game (js/Phaser.Game. 800 600 js/Phaser.CANVAS "example" #js {:create create}))
答案 0 :(得分:0)
Clojure不会在函数之后获取定义,因此它超出了范围。您可以将其放在范围内,将其移至功能上方,或将declare
放在文件顶部。