startDrag method

void startDrag(
  1. [bool lockCenter = false,
  2. Rectangle<num>? bounds]
)

Lets the user drag this sprite with the mouse or the current touch point.

If this method is called within the context of a touch event, the drag will be performed accordingly all future touch events with the same TouchEvent.touchPointID. Otherwise the drag will be performed based on mouse events.

The sprite remains draggable until explicitly stopped through a call to the stopDrag method.

With lockCenter you can specify whether the draggable sprite is locked to the center of the pointer position (true), or locked to the point where the user first clicked the sprite (false).

bounds is the value relative to the coordinates of the Sprite's parent that specify a constraint rectangle for the Sprite.

Implementation

void startDrag([bool lockCenter = false, Rectangle<num>? bounds]) {
  final stage = this.stage;
  final inputEvent = InputEvent.current;
  final globalPoint = Point<num>(0.0, 0.0);
  var anchorPoint = Point<num>(0.0, 0.0);
  var touchPointID = 0;

  if (inputEvent == null && stage != null) {
    globalPoint.copyFrom(stage.mousePosition);
  } else if (inputEvent is MouseEvent) {
    globalPoint.setTo(inputEvent.stageX, inputEvent.stageY);
  } else if (inputEvent is TouchEvent) {
    globalPoint.setTo(inputEvent.stageX, inputEvent.stageY);
    touchPointID = inputEvent.touchPointID;
  } else {
    return;
  }

  if (lockCenter) {
    anchorPoint = this.bounds.center;
  } else {
    globalToLocal(globalPoint, anchorPoint);
  }

  stage!._startDrag(this, globalPoint, anchorPoint, bounds, touchPointID);
}