0

我正在尝试使用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但显然需要 1 个参数。

我不明白为什么draw-state会以某种方式“转换”为 0-arity 函数。

4

1 回答 1

0

问题是我没有激活“功能(操作)模式”,所以绘图函数需要 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
于 2016-07-31T00:29:35.803 回答