• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

adobe animate drag and drop simple code

New Here ,
May 07, 2020 May 07, 2020

Copy link to clipboard

Copied

Creating a jigsaw puzzle like drag and drop game on Adobe Animate. Created it in action script 3.0 without any problems but am now trying to creat it in HTML5 and am struggling with the code. 

Does anyone have a code snippet or some simple code to create the drag and drop.  

Views

1.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2020 May 08, 2020

Copy link to clipboard

Copied

var puzzlepieces = [this.p1, this.p2,...];
var dropspots = [this.s1, this.s2,...];
var startA = [];
var startDrag = startDragF.bind(this);


for(var i=0;i<puzzlepieces.length;i++){
startA[i] = [puzzlepieces[i].x, puzzlepieces[i].y];
puzzlepieces[i].addEventListener("pressmove", startDrag);
puzzlepieces[i].addEventListener("pressup", stopDragF.bind(this));
}

function startDragF(e){
var p = stage.globalToLocal(e.stageX, e.stageY);
e.currentTarget.x = p.x;
e.currentTarget.y = p.y;
}
function stopDragF(e){
var i = puzzlepieces.indexOf(e.currentTarget);
var pt = e.currentTarget.localToLocal(0, 0, dropspots[i]);
if (dropspots[i].hitTest(pt.x, pt.y)) {
e.currentTarget.x = dropspots[i].x;
e.currentTarget.y = dropspots[i].y;
e.currentTarget.removeEventListener("pressmove", startDrag);
} else {
e.currentTarget.x = startA[i][0];
e.currentTarget.y = startA[i][1];
}
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

Hello,
Is it possible to modify this code so that the mouse does not automatically center on the moved occurrence?
Thanks in advance.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

yes, define an offset (between e.currentTarget.x,e.currentTarget.y and p.x,p.y) in startDrag.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

I recorded the position of the mouse at the time of the click by creating a mousedown. I don't know if it's possible to make it simpler but it works.

var puzzlepieces = [this.p1, this.p2];
var dropspots = [this.s1, this.s2, ];
var startA = [];
var startDrag = startDragF.bind(this);


for (var i = 0; i < puzzlepieces.length; i++) {
	startA[i] = [puzzlepieces[i].x, puzzlepieces[i].y];
	puzzlepieces[i].addEventListener("mousedown", startMouseDown.bind(this))
	puzzlepieces[i].addEventListener("pressmove", startDrag);
	puzzlepieces[i].addEventListener("pressup", stopDragF.bind(this));
}

var differenceX;
var differenceY;

function startMouseDown(e) {
	var pt = stage.globalToLocal(e.stageX, e.stageY);
	differenceX = (e.currentTarget.x - pt.x);
	differenceY = (e.currentTarget.y - pt.y);
}

function startDragF(e) {
	var p = stage.globalToLocal(e.stageX, e.stageY);
	e.currentTarget.x = p.x + differenceX;
	e.currentTarget.y = p.y + differenceY;
}


function stopDragF(e) {
	var i = puzzlepieces.indexOf(e.currentTarget);
	var pt = e.currentTarget.localToLocal(0, 0, dropspots[i]);
	if (dropspots[i].hitTest(pt.x, pt.y)) {
		e.currentTarget.x = dropspots[i].x;
		e.currentTarget.y = dropspots[i].y;
		e.currentTarget.removeEventListener("pressmove", startDrag);
	} else {
		e.currentTarget.x = startA[i][0];
		e.currentTarget.y = startA[i][1];
	}
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

Wow!!!  Thanks for this.  I was looking for coding to make a drag and drop game and this worked for me.  The only tweek I would like to make to it is to get the pieces to snap into place.  

 

This has been very helpful, thank you very much. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

use the code i suggested.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

Hi kglad, 

First of all, thank you for your quick response.  You've been a great help to me so please know that I value your input.  With that said, I tried your solution first, but all I got was the preloader.  I admit that I am not the brilliant coder that you are...by no  means.  But this is what I did:

 

  1. I created a new canvas file, and changed the Publish Settings to make it responsive and etc. 

2. Then I created 4 movie clips, named them p1, p2, s1, s2., and gave them the same instance names. 

 

Thank you for any assistance, 

Terri

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

Duh...I think that I am missing the opening code.  Can you help me with that?  

 

I admit, outside of Code Snippets, I am lost.  My strength is in the design process.       

 

Embarassed and humble, 

Terri

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

No, I didn't . Please disregard. Thnx!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

i'm not sure where you're at in this process.  is everything working the way you want?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

Hello, no. Nothing is working for me.  I am at the very beginning of the project so there was no coding done.  

 

The only thing I did was what was explained earlier. 

 

Presently, the only things on the stage are 4 movie clips named as they arein the coding;  p1, p2 and s1, s2.  I understand that p1 and p2 are names of  the puzzle piece to be dragged to the targets s1, and s2.  Isn't that correct?

 

I get stuck after that because I copied and pasted the code you provided. 

 

Thanks so much for your desire to help me, 

Terri

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

and...each movie clip has an instance name to match the puzzle piece name. 

 

Thanks

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

Mr. kglad, 

I figured it out and it is embarassing.  I literally copied the coding exactly as you typed it. 

 

After walking away from it, I realized that I neglected to delete the extra commas and periods ([this.p1, this.p2,...])

 

Thanks again for posting the code.  You are really appreciated. 

Regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2022 Mar 29, 2022

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines