Skip to content

Commit 701bec0

Browse files
committed
grab sprite under mouse at time of drag start
1 parent d32440d commit 701bec0

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/Project.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export default class Project {
2626

2727
public draggingSprite: Sprite | null;
2828
public dragThreshold: number;
29-
private _consideringDraggingSprite: Sprite | null;
3029
private _dragOffsetX: number;
3130
private _dragOffsetY: number;
3231
private _idleDragTimeout: number | null;
@@ -64,7 +63,6 @@ export default class Project {
6463

6564
this.answer = null;
6665
this.draggingSprite = null;
67-
this._consideringDraggingSprite = null;
6866
this._dragOffsetX = 0;
6967
this._dragOffsetY = 0;
7068
this._idleDragTimeout = null;
@@ -98,7 +96,6 @@ export default class Project {
9896
const spriteUnderMouse = this._spriteUnderMouse;
9997
const targetUnderMouse = this._targetUnderMouse;
10098
if (spriteUnderMouse && spriteUnderMouse.draggable) {
101-
this._consideringDraggingSprite = spriteUnderMouse;
10299
this._startIdleDragTimeout();
103100
} else {
104101
this._startClickTriggersFor(targetUnderMouse);
@@ -109,9 +106,9 @@ export default class Project {
109106
// TODO: Effects - goto() and moveAhead() - are applied immediately.
110107
// Do we want to buffer them to apply at the start of the next tick?
111108
if (this.input.mouse.down) {
112-
if (this._consideringDraggingSprite && this.input.mouse.downAt) {
113-
const distanceX = this.input.mouse.x - this.input.mouse.downAt.x;
114-
const distanceY = this.input.mouse.y - this.input.mouse.downAt.y;
109+
if (!this.draggingSprite) {
110+
const distanceX = this.input.mouse.x - this.input.mouse.downAt!.x;
111+
const distanceY = this.input.mouse.y - this.input.mouse.downAt!.y;
115112
const distanceFromMouseDown = Math.sqrt(
116113
distanceX ** 2 + distanceY ** 2
117114
);
@@ -226,26 +223,21 @@ export default class Project {
226223
}
227224

228225
private _startDragging(): void {
229-
if (!this.spritesAndClones.includes(this._consideringDraggingSprite)) {
230-
this._consideringDraggingSprite = null;
231-
}
226+
const spriteUnderMouse = this._spriteUnderMouse;
227+
if (!spriteUnderMouse || !spriteUnderMouse.draggable) return;
232228

233-
if (this._consideringDraggingSprite) {
234-
this.draggingSprite = this._consideringDraggingSprite;
235-
this._consideringDraggingSprite = null;
236-
this._clearIdleDragTimeout();
229+
this.draggingSprite = spriteUnderMouse;
230+
this._clearIdleDragTimeout();
237231

238-
this._dragOffsetX = this.draggingSprite.x - this.input.mouse.x;
239-
this._dragOffsetY = this.draggingSprite.y - this.input.mouse.y;
232+
this._dragOffsetX = this.draggingSprite.x - this.input.mouse.x;
233+
this._dragOffsetY = this.draggingSprite.y - this.input.mouse.y;
240234

241-
this.draggingSprite.moveAhead();
242-
}
235+
this.draggingSprite.moveAhead();
243236
}
244237

245238
private _clearDragging(): boolean {
246239
const wasDragging = !!this.draggingSprite;
247240
this.draggingSprite = null;
248-
this._consideringDraggingSprite = null;
249241
this._dragOffsetX = 0;
250242
this._dragOffsetY = 0;
251243
this._clearIdleDragTimeout();

0 commit comments

Comments
 (0)