Skip to main content
MarkSmit
Inspiring
October 4, 2022
Answered

Draggable object not aligned with cursor

  • October 4, 2022
  • 2 replies
  • 793 views

Hi everyone,

 

I'm making a test interactive animation. The goal of the interactive animation is that when you drag the blue ball over the red line an animation will play.

 

It works like a charm, the only problem I have is that the blue ball doesn't stay aligned with the cursor when I drag it. I guess it has to do something with getting the coordinates from the browser and not the stage? And this could be done with globalToLocal? But I don't know how to implement it to my code.

 

Example video: https://www.youtube.com/shorts/KCJ7yZphv5M

Project files: https://www.dropbox.com/scl/fo/h79cyghs9ebcab3qeaw7g/h?dl=0&rlkey=du4t4he9scsj7an45jl1cpgf6

 

this.stop();


let root = this;


this.pull_me.addEventListener("pressmove", dragCord)

function dragCord(e) {
	
	e.currentTarget.y= e.stageY;
}


this.pull_me.addEventListener("pressmove", activateSwitch)

function activateSwitch () {
	
	if (root.pull_me.y>852)
	root.gotoAndPlay(10);
}

 

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

 

This should work:

let root = this;

function onMouseDown(e)
{
	e.currentTarget.offset = { x: e.stageX / stage.scaleX - e.currentTarget.x, y: e.stageY / stage.scaleY - e.currentTarget.y };
}

function onPressMove(e)
{
	e.currentTarget.x = e.stageX / stage.scaleX - e.currentTarget.offset.x;
	e.currentTarget.y = e.stageY / stage.scaleY - e.currentTarget.offset.y;
	
	if (e.currentTarget.y > 852)
	{
		root.pull_me.removeEventListener("pressmove", onPressMove);
		root.gotoAndPlay(10);
	}
}

root.pull_me.addEventListener("mousedown", onMouseDown);
root.pull_me.addEventListener("pressmove", onPressMove);
root.stop();

 

I hope it helps.

 

Regards,

JC

2 replies

kglad
Community Expert
Community Expert
October 4, 2022

use:

 

e.currentTarget.y= e.stageY/stage.scaleY;
JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
October 4, 2022

Hi.

 

This should work:

let root = this;

function onMouseDown(e)
{
	e.currentTarget.offset = { x: e.stageX / stage.scaleX - e.currentTarget.x, y: e.stageY / stage.scaleY - e.currentTarget.y };
}

function onPressMove(e)
{
	e.currentTarget.x = e.stageX / stage.scaleX - e.currentTarget.offset.x;
	e.currentTarget.y = e.stageY / stage.scaleY - e.currentTarget.offset.y;
	
	if (e.currentTarget.y > 852)
	{
		root.pull_me.removeEventListener("pressmove", onPressMove);
		root.gotoAndPlay(10);
	}
}

root.pull_me.addEventListener("mousedown", onMouseDown);
root.pull_me.addEventListener("pressmove", onPressMove);
root.stop();

 

I hope it helps.

 

Regards,

JC

kglad
Community Expert
Community Expert
October 4, 2022

@JoãoCésar17023019, do you know why or how the stage is scaled (up by 25%), by default!?

JoãoCésar17023019
Community Expert
Community Expert
October 4, 2022

Hello, @kglad! What exactly do you mean?