Skip to main content
Inspiring
July 26, 2020
Question

Instance "jumps" to mouse position

  • July 26, 2020
  • 2 replies
  • 319 views

Hello, guys!
I would like to know how to correct the code to prevent "jump" of instance (Y coords) to initial Mouse Down position. What should I add?

 

this.L.y = 430;
this.L.on("pressmove", function(evt) {
   let p = evt.currentTarget.parent.globalToLocal(evt.stageX, evt.stageY);
   //evt.currentTarget.x = p.x;
   evt.currentTarget.y = p.y;
   if (evt.currentTarget.y > 730) {
      evt.currentTarget.y = 730;
   } else if (evt.currentTarget.y < 430) {
      evt.currentTarget.y = 430;
   }
stage.update();
});

 

Thank you!

This topic has been closed for replies.

2 replies

Inspiring
July 27, 2020

I've fixed the problem in a such way. It works perfectly on iMac, but then doesn't work on iPad.

What's the problem?

 

createjs.Touch.enable(stage);

this.L.y = 430;
let offset = 0;

 

this.L.addEventListener("mousedown", (evt) => {
let a = evt.currentTarget.parent.globalToLocal(evt.stageX, evt.stageY);
offset = a.y - evt.currentTarget.y;
console.log('mouse clicked');
});

this.L.addEventListener("pressmove", (evt) => {
let p = evt.currentTarget.parent.globalToLocal(evt.stageX, evt.stageY);
//evt.currentTarget.x = p.x;
evt.currentTarget.y = p.y - offset;
if (evt.currentTarget.y > 730) {
evt.currentTarget.y = 730;
} else if (evt.currentTarget.y < 430) {
evt.currentTarget.y = 430;
}
stage.update();
});

this.L.addEventListener("touchstart",function(evt) {
evt.preventDefafult();
let a = evt.currentTarget.parent.globalToLocal(evt.stageX, evt.stageY);
offset = a.y - evt.currentTarget.y;
console.log('touched');
});

this.L.addEventListener("touchmove",function(evt) {
evt.preventDefafult();
let p = evt.currentTarget.parent.globalToLocal(evt.stageX, evt.stageY);
//evt.currentTarget.x = p.x;
evt.currentTarget.y = p.y - offset;
if (evt.currentTarget.y > 730) {
evt.currentTarget.y = 730;
} else if (evt.currentTarget.y < 430) {
evt.currentTarget.y = 430;
}
stage.update();
});

Legend
July 27, 2020

"Doesn't work" is uselessly vague. What exactly do you mean?

Legend
July 26, 2020

Capture the X/Y difference between the mouse coordinates and the clicked symbol to a set of persistent variables, then subtract (or add) that difference when setting the symbol's dragged coordinates.

Inspiring
July 27, 2020

Sure, I have done this with mouseclick. Then mouse down. How to do this within one code block?