-
How to addChild from an array of MC's based on e.target?
I have an array of 8 movie clips which can be dragged and dropped onto an MC that is their common hitObject.
I would like whichever mc is being dragged to be added as the child of the hitObject MC when it is dropped, however I am having troubles setting up the code.
Currently, I can only have one specific instance added as a child of the hitObject MC because I don't know what to write inside the addChild() parameter aside from a specific instance name (neither "e.target" nor the array name are acceptable).
Here's my code --any and all help will be most appreciated:
Code:
var redArray:Array = [red,red1,red2,red3,red4,red5,red6,red7];
redArray.forEach(setupDrag);
function setupDrag(dragger:MovieClip, index:int, array:Array):void {
dragger.addEventListener(MouseEvent.MOUSE_DOWN, dragRed);
dragger.buttonMode=true;}
redArray.forEach(setupDrop);
function setupDrop(dropper:MovieClip, index:int, array:Array):void {
dropper.addEventListener(MouseEvent.MOUSE_UP, dropRed);
dropper.buttonMode=true;}
function dragRed(e:Event):void{
e.target.startDrag();}
function dropRed(e:Event):void{
e.target.stopDrag();
if (e.target.hitTestObject(drawer_mc))
{
drawer_mc.addChild(red1); // THIS LINE IS MY PROBLEM --WHAT TO REPLACE red1 WITH?
red1.y=10;
}
}
Thanks in advance!
-
Resolved!
After receiving a TypeError: Error #1007: Instantiation attempted on a non-constructor, I found this thread which gave me what I needed to solve the problem of what to put in the addChild () parameters in order to have whatever e.target was being dragged be added as child.
Here's what I added to the if part of my code:
if (e.target.hitTestObject(drawer_mc))
{
var newObject:MovieClip = new e.currentTarget.constructor();
newObject.x = e.currentTarget.x;
newObject.y = e.currentTarget.y;
drawer_mc.addChild(newObject);
newObject.y=0;
e.target.visible=false;
}
-
How to write a code in AS3, that places letters from library to stage by pressing keyboard. Alphabetic letters are symbols.
IF THERE ANY way to place letters on stage only once?
Thank you
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|