From e34ffbd20d002104cfb65034f0ff86b301e17306 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Sat, 2 Jul 2022 09:03:28 -0500 Subject: [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. --- examples/06-sprite.lisp | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) (limited to 'examples') 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))) -- cgit v1.2.3