Copy link to clipboard
Copied
Hi everyone,
I have a question regarding whether or not this idea is feasible. If I have a drag and drop game, with individiual movie clips, could I pack them together so the user would pick up a cluster of mcs, drop them, and they would seperate and become individual again? My thought process on this is to create a holder transparent mc with all the mcs inside of it. Pick it up as a whole, place it and somehow delete the holder mc (parent maybe), and promote the mc's to parent status? If anyone can help direct me that'd be great.
Copy link to clipboard
Copied
hey ken,
I dropped the code in the unpack function area and get a different error in each area I put it in. I did remove the initial event listeners. At what section of the code did you mean?
Copy link to clipboard
Copied
Sorry it's inside while(--i) loop
Copy link to clipboard
Copied
ok you've got much more talent here then I do, I put it here. Now the behavior on the output was odd. When I dragged the pile into the unpack area. the pile container remained in the unpackzone still visible, poverty unpacked and moved, and the other two mcs disappeared.
function unpackpile(e:DisplayObject):void{
if(unpackzone.hitTestPoint(mouseX, mouseY, true)){
var i:uint = pile1.numChildren;
while (--i) {
var movieclip:MovieClip = addChild(pile1.removeChild(pile1.getChildAt(i))) as MovieClip;
movieclip.addEventListener(MouseEvent.MOUSE_DOWN, grabMe);
trace(addChild(pile1.removeChild(pile1.getChildAt(i))));
}
}
}
Copy link to clipboard
Copied
Remove the trace
Copy link to clipboard
Copied
ok I've attached a screenshot. Poverty becomes moveable, but the two other mcs completely disappear, and the container (pile1) remains visible where I let go of the mouse in the unpack zone. I removed the trace as well.
Copy link to clipboard
Copied
...
var i:uint = pile1.numChildren;
while (--i) {
var movieclip:MovieClip = addChild(pile1.removeChild(pile1.getChildAt(i))) as MovieClip;
movieclip.addEventListener(MouseEvent.MOUSE_DOWN, grabMe);
}removeChild(pile1);
...
Copy link to clipboard
Copied
now we have a winner thanks for your help ken. Now if I wanted to create multiple piles, would I use the same code but with new functions and mc name adaptations?
Copy link to clipboard
Copied
You can make the function more generic so that you can use it for any number of piles, i.e.
unpackpile(e.currentTarget as MovieClip);
...
function unpackpile(pile:MovieClip):void {
...
var i:uint = pile.numChildren;
...
}
Copy link to clipboard
Copied
now heres where it gets messy in theory. So say I have 3 containers, each will have mcs added to them (pile1, pile2, and pile3). So the adding of mcs, no problem. But if I make the code for the unpack generic, how will flash know to only unpack things denoted as pile1-3, not the individual mcs?
now your use of the word "pile" in the code, would that be a class assigned to all mcs pile1, pile2, pile3, etc?
Copy link to clipboard
Copied
You call the function with a parameter (a MovieClip to be "unpacked") so that Flash knows what to unpack.
unpackpile(pile1);
unpackpile(pile2);
etc...
Copy link to clipboard
Copied
hey ken, so with this it looks good, the pile container though will not remove itself, the items though are active. So very close! How would I ensure the container is removed once unpacked? The exercise operates but I do get a stack overlow message in the output tab.
backbutton.addEventListener(MouseEvent.CLICK, backclick);
helpbutton.addEventListener(MouseEvent.CLICK, helpclick);
answerbutton.addEventListener(MouseEvent.CLICK, answerclick);
pile1.addEventListener(MouseEvent.MOUSE_DOWN, grabMe);
pile2.addEventListener(MouseEvent.MOUSE_DOWN, grabMe);
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.media.Sound;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.display.StageDisplayState;
pile1.addChild(cash);
cash.x=0
cash.y=0
pile1.addChild(ineffectiveness);
ineffectiveness.x=0
ineffectiveness.y=10
pile1.addChild(poverty);
poverty.x=0
poverty.y=5
pile2.addChild(sex);
sex.x=0
sex.y=0
pile2.addChild(love);
love.x=0
love.y=10
pile2.addChild(loneliness);
loneliness.x=0
loneliness.y=5
var me:Object;
function grabMe(e:MouseEvent):void{
me = e.currentTarget;
me.removeEventListener(MouseEvent.MOUSE_DOWN, grabMe);
me.startDrag();
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragMe);
stage.addEventListener(MouseEvent.MOUSE_UP, dropMe);
}
function dropMe(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, dropMe);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragMe);
me.stopDrag();
me.addEventListener(MouseEvent.MOUSE_DOWN, grabMe);
unpackpile(e.currentTarget as MovieClip);
}
function dragMe(e:MouseEvent):void {
e.updateAfterEvent();
}
function unpackpile(pile:MovieClip):void {
if(unpackzone.hitTestPoint(mouseX, mouseY, true)){
var i:uint = me.numChildren;
while (--i) {
var movieclip:MovieClip = addChild(me.removeChild(me.getChildAt(i))) as MovieClip;
movieclip.addEventListener(MouseEvent.MOUSE_DOWN, grabMe);
}
unpackpile(pile1);
unpackpile(pile2);
}
}
function backclick(event:MouseEvent):void{
gotoAndStop("main");
}
function helpclick(event:MouseEvent):void{
gotoAndStop("help");
}
function answerclick(event:MouseEvent):void{
gotoAndStop("answer");
}
Copy link to clipboard
Copied
Replace the following in your function unpackpile(pile:MovieClip):void
unpackpile(pile1);
unpackpile(pile2);
to
removeChild(pile);
You should also check if the dropped MovieClip is pile1/pile2 before unpacking.
Copy link to clipboard
Copied
With the change to removechild the items still unpack properly but the container pile still remains. In my output I get type error 2007: parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at As3test_fla::MainTimeline/unpackpile()
at As3test_fla::MainTimeline/dropMe()
Copy link to clipboard
Copied
Well you're mixing "me" in - then try removeChild(me);
instead
Copy link to clipboard
Copied
i went with "me" as it is utilized in the drag/drop for the active item. When I switch that out for me I get an error, 1118 implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.
I've been messing with different things. The functionality is close just not sure why I get output errors and the container remains...
Copy link to clipboard
Copied
Change the following:
From:
var me:Object;
to:
var me:MovieClip;
from:
me = e.currentTarget;
to:
me = e.currentTarget as MovieClip;
Copy link to clipboard
Copied
that works....thanks for all your help ken. I can retire....