1 Replies - 2163 Views - Last Post: 01 February 2016 - 09:32 AM

#1 abhimabi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-January 16

Drag and Drop allow only one object per target in as2 and more

Posted 30 January 2016 - 11:01 AM

I am a beginner in actionscript,
I am learning by beating bush around so please forgive me if anything wrong i said
Please guide me i am using actionscript 2

1.How can I shorten this code
2.How Drag and Drop will allow only one movieclip per target
3.I am practicing to achieve a goal that is to learn actionscript and the code is all about that.
4.There are 6 targets and 4 drag-able movieclips, I need to put the four drag-ables to any 6 targets and target should snap the drag-able's, there cant be two dragables in one target area, once all the drag-ables are dropped to target submit button will be enabled before that it will be disabled. Submit button will check the correct dragables are placed on to correct targets or not. If place it will go to correct feedback.

stop ();

btn_Submit.enabled = false;

var dRoot = this;

var x1pos = drag1._x;
var y1pos = drag1._y;
var x2pos = drag2._x;
var y2pos = drag2._y;
var x3pos = drag3._x;
var y3pos = drag3._y;
var x4pos = drag4._x;
var y4pos = drag4._y;


var pressed = 0;

drag (drag1);
drag (drag2);
drag (drag3);
drag (drag4);

function timeOut ()
{
	pressed = 0;
	clearInterval (time);
}


//drag1

drag1.onPress = function ()
{
	pressed++;
	if (pressed == 1)
	{
		time = setInterval (timeOut, 200);
		startDrag (this);
	}
	else
	{
		timeOut ();
		dRoot.drag1._x = x1pos;
		dRoot.drag1._y = y1pos;
	}
};

drag1.onRelease = drag1.onReleaseOutside = function ()
{
	stopDrag ();
	
	//snap to each drop
	curdrop = new MovieClip ();
	if (this.hitTest (dRoot["drop1"]))
	{
		curdrop = dRoot["drop1"];
		
	}
	else if (this.hitTest (dRoot["drop2"]))
	{
		curdrop = dRoot["drop2"];
	}
	else if (this.hitTest (dRoot["drop3"]))
	{
		curdrop = dRoot["drop3"];
	}
	else if (this.hitTest (dRoot["drop4"]))
	{
		curdrop = dRoot["drop4"];
	}
	else if (this.hitTest (dRoot["drop5"]))
	{
		curdrop = dRoot["drop5"];
	}
	else if (this.hitTest (dRoot["drop6"]))
	{
		curdrop = dRoot["drop6"];
	}
	this._x = dRoot.curdrop._x;
	this._y = dRoot.curdrop._y;
};

//drag1 done

//drag2

drag2.onPress = function ()
{
	pressed++;
	if (pressed == 1)
	{
		time = setInterval (timeOut, 200);
		startDrag (this);
	}
	else
	{
		timeOut ();
		dRoot.drag2._x = x2pos;
		dRoot.drag2._y = y2pos;
	}

};

drag2.onRelease = drag2.onReleaseOutside = function ()
{
	stopDrag ();
	//snap to each drop
	curdrop = new MovieClip ();
	if (this.hitTest (dRoot["drop1"]))
	{
		curdrop = dRoot["drop1"];
	}
	else if (this.hitTest (dRoot["drop2"]))
	{
		curdrop = dRoot["drop2"];
	}
	else if (this.hitTest (dRoot["drop3"]))
	{
		curdrop = dRoot["drop3"];
	}
	else if (this.hitTest (dRoot["drop4"]))
	{
		curdrop = dRoot["drop4"];
	}
	else if (this.hitTest (dRoot["drop5"]))
	{
		curdrop = dRoot["drop5"];
	}
	else if (this.hitTest (dRoot["drop6"]))
	{
		curdrop = dRoot["drop6"];
	}
	this._x = dRoot.curdrop._x;
	this._y = dRoot.curdrop._y;
};

//drag2 done

//drag3

drag3.onPress = function ()
{
	pressed++;
	if (pressed == 1)
	{
		time = setInterval (timeOut, 200);
		startDrag (this);
	}
	else
	{
		timeOut ();
		dRoot.drag3._x = x3pos;
		dRoot.drag3._y = y3pos;
	}
};

drag3.onRelease = drag3.onReleaseOutside = function ()
{
	stopDrag ();
	//snap to each drop
	curdrop = new MovieClip ();
	if (this.hitTest (dRoot["drop1"]))
	{
		curdrop = dRoot["drop1"];
		
	}
	else if (this.hitTest (dRoot["drop2"]))
	{
		curdrop = dRoot["drop2"];
	}
	else if (this.hitTest (dRoot["drop3"]))
	{
		curdrop = dRoot["drop3"];
	}
	else if (this.hitTest (dRoot["drop4"]))
	{
		curdrop = dRoot["drop4"];
	}
	else if (this.hitTest (dRoot["drop5"]))
	{
		curdrop = dRoot["drop5"];
	}
	else if (this.hitTest (dRoot["drop6"]))
	{
		curdrop = dRoot["drop6"];
	}
	this._x = dRoot.curdrop._x;
	this._y = dRoot.curdrop._y;
};

//drag3 done

//drag4

drag4.onPress = function ()
{
	pressed++;
	if (pressed == 1)
	{
		time = setInterval (timeOut, 200);
		startDrag (this);
	}
	else
	{
		timeOut ();
		dRoot.drag4._x = x4pos;
		dRoot.drag4._y = y4pos;
	}
};

drag4.onRelease = drag4.onReleaseOutside = function ()
{
	stopDrag ();
	//snap to each drop
	curdrop = new MovieClip ();
	if (this.hitTest (dRoot["drop1"]))
	{
		curdrop = dRoot["drop1"];
	}
	else if (this.hitTest (dRoot["drop2"]))
	{
		curdrop = dRoot["drop2"];
	}
	else if (this.hitTest (dRoot["drop3"]))
	{
		curdrop = dRoot["drop3"];
	}
	else if (this.hitTest (dRoot["drop4"]))
	{
		curdrop = dRoot["drop4"];
	}
	else if (this.hitTest (dRoot["drop5"]))
	{
		curdrop = dRoot["drop5"];
	}
	else if (this.hitTest (dRoot["drop6"]))
	{
		curdrop = dRoot["drop6"];
	}
	this._x = dRoot.curdrop._x;
	this._y = dRoot.curdrop._y;
};

//drag4 done

//hittest

target_mc = ["drop1", "drop2", "drop3", "drop4", "drop5", "drop6"];

drag_mc = ["drag1", "drag2", "drag3", "drag4"];

function hitTester ()
{
	numCircles = target_mc.length;
	for (i = 0; i < numCircles; i++)
	{
		circleA = target_mc[i];
		for (j = i + 1; j < numCircles; j++)
		{
			circleB = drag_mc[j];
			temp_A = eval (circleA);
			temp_B = eval (circleB);
			if (temp_A.hitTest (temp_B)/>/>)
			{				
			btn_Submit.enabled = true;
			}

		}
	}
}
this.onEnterFrame = function() {
hitTester();}

//submit button


btn_Submit.onRelease = function ()
{

	if (eval (dRoot.drag1._droptarget) == drop1 
					&& eval (dRoot.drag2._droptarget) == drop2 
					&& eval (dRoot.drag3._droptarget) == drop3 
					&& eval (dRoot.drag4._droptarget) == drop4)
				{
					dRoot.gotoAndPlay ("feedbackCorrect");
				}
				else
				{
					dRoot.gotoAndPlay ("feedbackIncorrect");
				}

};



Hoping for answer Thank you
Abhimabi

Is This A Good Question/Topic? 0
  • +

Replies To: Drag and Drop allow only one object per target in as2 and more

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2811
  • View blog
  • Posts: 8,162
  • Joined: 15-January 14

Re: Drag and Drop allow only one object per target in as2 and more

Posted 01 February 2016 - 09:32 AM

Quote

I am learning by beating bush around

That's not the correct way to say that, but I like your way better.

Quote

How can I shorten this code

You could use a loop to apply those drag and drop handlers to each item, but a better way would be to create the draggable objects as movie clips in your library, and put the code to handle the drag and drop on the library object. That way you only have 1 piece of code for all draggable objects and it's defined in the library. You can create 4 instances of the library object to use as your draggable objects on the stage.

Quote

How Drag and Drop will allow only one movieclip per target

Use properties on the targets. When you drag an item to a target, you can save a reference to the target in the draggable, and you can save a reference to the draggable in the target. So in your onrelease function after you do the hittest, you can save curdrop as a property on the draggable, e.g. this.dropTarget = curdrop;, and in the target you can save a reference to the draggable and the fact that it has an item in it, e.g. curdrop.dragItem = this; curdrop.dragFilled = true;. When you drop an item and use hittest to get the target then you can check the dragFilled property to see if it already has an item in it. You can name those properties whatever you want as long as it's not a name that's already being used.

Quote

once all the drag-ables are dropped to target submit button will be enabled

In the drop handler, once the item gets dropped in the target then you can loop through all of the draggables to check if each one is in a target, or you can loop through the targets and count the number that have an item in them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1