我正在尝试使用Quil编写我的第一个简单图形应用程序。现在,我只是想画一个在屏幕上对角移动的点。
(ns quil-test.quil-first
(:require [quil.core :as q])
(:gen-class))
(defn setup-state []
(q/frame-rate 60)
{:x 0})
(defn update-state [s]
(assoc s :x (q/frame-count)))
(defn draw-state [state]
(let [x (:x state)]
(q/stroke-weight 100)
(q/point x x)))
(q/defsketch quil-first
:size [500 500]
:setup setup-state
:update update-state
:draw draw-state)
问题是,运行它不会显示一个点,并且以下错误会重复打印到控制台:
Exception in :draw function: #error {
:cause Wrong number of args (0) passed to: quil-first/draw-state
:via
[{:type clojure.lang.ArityException
:message Wrong number of args (0) passed to: quil-first/draw-state
:at [clojure.lang.AFn throwArity AFn.java 429]}]
:trace
[[clojure.lang.AFn throwArity AFn.java 429]
[clojure.lang.AFn invoke AFn.java 28]
[clojure.lang.Var invoke Var.java 375]
[quil.middlewares.safe_fns$wrap_fn$fn__114 invoke safe_fns.clj 9]
[quil.middlewares.bind_output$bind_output$iter__148__152$fn__153$fn__164 invoke bind_output.clj 21]
[quil.applet$_draw invoke applet.clj 220]
[quil.Applet draw nil -1]
[processing.core.PApplet handleDraw PApplet.java 2402]
[quil.Applet handleDraw nil -1]
[processing.awt.PSurfaceAWT$12 callDraw PSurfaceAWT.java 1527]
[processing.core.PSurfaceNone$AnimationThread run PSurfaceNone.java 316]]}
stacktrace: clojure.lang.ArityException: Wrong number of args (0) passed to: quil-first/draw-state
at clojure.lang.AFn.throwArity (AFn.java:429)
clojure.lang.AFn.invoke (AFn.java:28)
clojure.lang.Var.invoke (Var.java:375)
quil.middlewares.safe_fns$wrap_fn$fn__114.invoke (safe_fns.clj:9)
quil.middlewares.bind_output$bind_output$iter__148__152$fn__153$fn__164.invoke (bind_output.clj:21)
quil.applet$_draw.invoke (applet.clj:220)
quil.Applet.draw (:-1)
processing.core.PApplet.handleDraw (PApplet.java:2402)
quil.Applet.handleDraw (:-1)
processing.awt.PSurfaceAWT$12.callDraw (PSurfaceAWT.java:1527)
processing.core.PSurfaceNone$AnimationThread.run (PSurfaceNone.java:316)
这意味着我的draw-state
函数需要0个参数,而它应该需要1个参数。draw-state
显然接受了一个论点。
我不明白为什么draw-state
会以某种方式被"转换"为0-arity函数。
问题是我没有激活"fun(ctional)mode",所以draw函数需要0个参数,因为默认情况下状态是全局管理的。
这项工作:
(ns quil-test.quil-first
(:require [helpers.general-helpers :as g]
[quil.core :as q]
[quil.middleware :as m])
(:gen-class))
(defn setup-state []
(q/frame-rate 60)
{:x 0})
(defn update-state [s]
(g/update-with s :x (fn [_] (q/frame-count))))
(defn draw-state [state]
(let [x (:x state)]
(q/stroke-weight 100)
(q/point x x)))
(q/defsketch quil-first
:size [500 500]
:setup setup-state
:update update-state
:draw draw-state
:middleware [m/fun-mode]) ; Activate functional mode