PencilCoder

Event Polling!

Goals:

  • Learn about polling, a powerful idiom for working with events
  • Explore algorithms that move sprites fluidly in response to end-user input

Coding Snippets:

  • coding snippets
  • Pencil Code gives direct access to the most-recently-generated mouse event objects with variables lastclick, lastmouseup, lastmousedown, and lastmousemove.
  • The pressed function can be used to check whether a specific key has been pressed (as shown in the example) or to list all currently-pressed keys by calling pressed().

Math and Computer Concepts:

  • Pencil Code maintains records of the most recent event object for several mouse-related events, as well as a list of all currently pressed keys. It does this, behind the scenes, using event listeners.
  • Referencing these records of events in a frequent, repeated manner—i.e., within the body of a callback to a timing event—is called polling.
  • Polling allows us to write code that responds more fluidly to events, owing to the fact that the timer dictates the timing interval of the execution of our callback, rather than the timing of the mouse or keyboard event.

Activities:

Save each of the following in a folder called EventPolling.