diff options
author | Colin Okay <colin@cicadas.surf> | 2022-07-02 09:03:28 -0500 |
---|---|---|
committer | Colin Okay <colin@cicadas.surf> | 2022-07-02 09:03:28 -0500 |
commit | e34ffbd20d002104cfb65034f0ff86b301e17306 (patch) | |
tree | db7f9c88331bfa6aa216fe955c9d1a89edd6d08c /examples | |
parent | d618184156f734b2e9aa4ec7d1bc3d0bfe274722 (diff) |
[example] added a second keydown handler to sprite example
In order to control speed, but also to demonstrate that you can add as
many handlers for an event as you like.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/06-sprite.lisp | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/examples/06-sprite.lisp b/examples/06-sprite.lisp index 57916a5..2addfd7 100644 --- a/examples/06-sprite.lisp +++ b/examples/06-sprite.lisp @@ -8,7 +8,8 @@ (defclass sprite-example (ww::application) ()) (defclass dude (ww::sprite) - ((walkingp :accessor walkingp :initform nil))) + ((walkingp :accessor walkingp :initform nil) + (walking-speed :accessor walking-speed :initform 5))) (defun set-key-if-not (sprite key ) "Sets the frame kyey for sprite if that key is not already set. Also @@ -24,30 +25,41 @@ (ww::defhandler move-by-face (ww::on-perframe (sprite) "When the sprite is walking, adjust its position." - (when (walkingp sprite) - (case (ww::frameset-key sprite) - (:left - (decf (ww::x sprite) +walking-speed+)) - (:right - (incf (ww::x sprite) +walking-speed+)) - (:back - (incf (ww::y sprite) +walking-speed+)) - (:front - (decf (ww::y sprite) +walking-speed+)))))) + (with-slots (walkingp walking-speed) sprite + (when walkingp + (case (ww::frameset-key sprite) + (:left + (decf (ww::x sprite) walking-speed)) + (:right + (incf (ww::x sprite) walking-speed)) + (:back + (incf (ww::y sprite) walking-speed)) + (:front + (decf (ww::y sprite) walking-speed))))))) (ww::defhandler move-dude - (ww::on-keydown (target scancode) + (ww::on-keydown (sprite scancode) "Set sprite to walking and set the frameset appropriate to its direction." - (setf (walkingp target) t) (case scancode (:scancode-left - (set-key-if-not target :left)) + (set-key-if-not sprite :left) + (setf (walkingp sprite) t)) (:scancode-right - (set-key-if-not target :right)) + (set-key-if-not sprite :right) + (setf (walkingp sprite) t)) (:scancode-up - (set-key-if-not target :back)) + (set-key-if-not sprite :back) + (setf (walkingp sprite) t)) (:scancode-down - (set-key-if-not target :front))))) + (set-key-if-not sprite :front) + (setf (walkingp sprite) t))))) + +(ww::defhandler speed-control + (ww::on-keydown (sprite scancode) + "A second keydown handler, for controlling walking speed. " + (case scancode + (:scancode-u (incf (walking-speed sprite))) + (:scancode-d (decf (walking-speed sprite)))))) (ww::defhandler stand (ww::on-keyup (target) @@ -102,6 +114,7 @@ (ww::add-handler dude #'move-by-face ) (ww::add-handler dude #'move-dude) (ww::add-handler dude #'stand) + (ww::add-handler dude #'speed-control) (ww::refocus-on dude) (ww::add-unit app dude))) |