- override
Handles an event, such as a pointer (touch or mouse) event.
Override this method to handle events. The node will only receive events if the userInteractionEnabled
property
is set to true and the isPointInside
method returns true for the position of the pointer down event (default
behavior provided by NodeWithSize
). Unless handleMultiplePointers
is set to true, the node will only receive
events for the first pointer that is down.
Return true if the node has consumed the event, if an event is consumed it will not be passed on to nodes behind the current node.
// MyTouchySprite gets transparent when we touch it
class MyTouchySprite extends Sprite {
MyTouchySprite(Image img) : super (img) {
userInteractionEnabled = true;
}
bool handleEvent(SpriteBoxEvent event) {
if (event.type == PointerDownEvent) {
opacity = 0.5;
}
else if (event.type == PointerUpEvent) {
opacity = 1.0;
}
return true;
}
}
Source
@override bool handleEvent(SpriteBoxEvent event) { if (event.type == PointerDownEvent) { _pointerDownAt = event.boxPosition; actions.stopAll(); _isDown = true; } else if (event.type == PointerUpEvent || event.type == PointerCancelEvent) { _pointerDownAt = null; _value = Point.origin; ActionTween moveToCenter = new ActionTween((Point a) => _handlePos = a, _handlePos, _center, 0.4, Curves.elasticOut); actions.run(moveToCenter); _isDown = false; } else if (event.type == PointerMoveEvent) { Offset movedDist = event.boxPosition - _pointerDownAt; _value = new Point( (movedDist.dx / 80.0).clamp(-1.0, 1.0), (movedDist.dy / 80.0).clamp(-1.0, 1.0)); _handlePos = _center + new Offset(_value.x * 40.0, _value.y * 40.0); } return true; }