Events
One way of thinking of user interaction is that the user is sending events to the nodes. For example, when the user clicks on a button, the button receives a TapDown
event. The button can then respond to the event by executing code. The code passed into the onTapDown()
method is called an event handler because it handles the TapDown
event.
This event handler prints a message to the console when the button is tapped:
myButton.onTapDown(() => {
console.log("TapDown event!");
});
Event handlers, callback functions, and () =>
There are several types of events that can be sent to nodes:
TapDown
- occurs when the user begins a touch or click within the bounds of the node.TapUp
- occurs when the user releases a touch or click within the bounds of the node.TapLeave
- occurs when the user holds a touch or click on the node, but moves outside the bounds of the node.TapUpAny
- occurs when the user releases a touch or click within or outside the bounds of the node.PointerDown
- occurs when the user begins a touch or click within the bounds of the node. Same asTapDown
.PointerUp
- occurs when the user releases a touch or click within the bounds of the node, but the touch or click did not have to begin within the bounds of the node.PointerMove
- occurs when the user moves the mouse or touch within the bounds of the node.
tip
The variety of tap and pointer events may seem confusing, and the difference between a tap and a pointer may also be hard to understand. In most cases, you'll just need the TapDown
and TapUp
events. The other events are useful for more advanced scenarios.