FORUMS: list search recent posts

need advice on removing something from the stage

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Denise Gross
need advice on removing something from the stage
on Oct 8, 2011 at 7:38:13 pm

Hi all,
I am pretty much a Flash nubie just forging my way through things. I am making a little drag and drop game where you click on a movieclip, which acts as a button. When the moviclip/button is clicked a duplicate of that same movieclip, only larger, is added to the stage. You then drag and drop it in a designated area on the stage.

I have that part of the code working fine. What I need is a reset button that removes the larger movieclip that was dragged. I am not sure how to do this.

Here is my code that is working fine:

myMC.addEventListener(MouseEvent.MOUSE_DOWN, moveMC);


function moveMC(e:MouseEvent){
var newMC:MyMC = new MyMC();
addChild(newMC);
newMC.width += 280;
newMC.height += 280;
newMC.x=myMC.x;
newMC.y=myMC.y;

newMC.addEventListener(MouseEvent.MOUSE_DOWN, dragMC)
newMC.addEventListener(MouseEvent.MOUSE_UP, dropMC)
}



function dragMC(e:MouseEvent){
e.currentTarget.startDrag();
}

function dropMC(e:MouseEvent){
e.currentTarget.stopDrag();
}


I tried removeChild for (myMC); and that removed everything. I only want to remove the second larger duplicate movieclip.

Anyhelp would be greatly appreciated!


Return to posts index

demetri tashie
Re: need advice on removing something from the stage
on Oct 10, 2011 at 1:56:50 am

your idea is close, but as you saw, that would remove the original button "myMC". i don't see that it would actually remove everything though, just the myMC' buttonitself.

instead, since you want to remove the large created clip "newMC", you need to have code saying:
"removeChild(newMC);"

a few caveats: mentioning that in code before the actual clip is created will throw errors. it is better to at least declare the clip outside of the function, so it can be safely refernced in code.

i adjusted the code ( see below) to accomodate for the reset button ( "restBtn"), and also made sure that a) the reset button is only listening for a click when the 'newMC' clip is actually added to the stage, and b) the original button can only be clicked if there is no 'newMC' on the stage. this also ensures there will be no errors thrown. if you actually want to or expect to be able to create multiple large clips at once, you will need to adjust the code.
myMC.addEventListener(MouseEvent.MOUSE_DOWN, moveMC);

var newMC:MovieClip; // now declared ()and type cast) outside the function

function moveMC(e:MouseEvent){
newMC = new MyMC(); // instantiated here
addChild(newMC);
newMC.width += 280;
newMC.height += 280;
newMC.x=myMC.x;
newMC.y=myMC.y;

newMC.addEventListener(MouseEvent.MOUSE_DOWN, dragMC) ;
newMC.addEventListener(MouseEvent.MOUSE_UP, dropMC);

resetBtn.addEventListener(MouseEvent.CLICK, removeMC); // add listener
}


function dragMC(e:MouseEvent){
e.currentTarget.startDrag();
}

function dropMC(e:MouseEvent){
e.currentTarget.stopDrag();
myMC.removeEventListener(MouseEvent.MOUSE_DOWN, moveMC); //remove listener
}


function removeMC(e:MouseEvent){
removeChild(newMC);
resetBtn.removeEventListener(MouseEvent.CLICK, removeMC); // removes
myMC.addEventListener(MouseEvent.MOUSE_DOWN, moveMC); // adds
}


i hope that helps you


Return to posts index

Denise Gross
Re: need advice on removing something from the stage
on Oct 10, 2011 at 10:27:46 am

Wow! Thanks you so much for your response.

I see what you mean about creating the clip outside of the function.

It is working fine now.

Thanks again, I really appreciate it!


Return to posts index


Denise Gross
Re: need advice on removing something from the stage
on Oct 10, 2011 at 2:17:05 pm

Actually, if I did create multiple different large clips at once how would I adjust the code for the reset button?

As always, any response is greatly appreciated!


Return to posts index

demetri tashie
Re: need advice on removing something from the stage
on Oct 12, 2011 at 3:18:31 pm

are you creating multiple instances at once? or one at a time?
are you wanting to remove ALL the created objects at once, or one at a time?

one approach is to use removeChildAt() to remove child objects.

objects are added to the 'display list' in order. the first object on the stage is at depth 0, the second at depth 1 etc.
so if you have your 'myMC' and 'resetBtn' on the stage, they are at depth 0 and 1. your first ' newMC' added is at depth 2. next one added is at depth 3, and so on.

if you create a variable - a number ( lets use a uint)- you can keep track of the children on the stage. it could be:

var i:uint=1;

i started it at one becuase i knew that before anything else is added, the last thing the stage has on it is at depth 1 . that is, it has the 'myMC' and 'resetBtn' at depthes 0 and 1. your situation may differ.

after you addChild(newMC); you then increment the variable ' i ' by one:
i++;

now, if you want to remove them all, but one by one- in reverse order as they were created-, the resetBtn function could have this:
if ( i >=2 ){ removeChildAt(i); i--; }
so each click will remove the highest numbered child,then decrease the variable, preparing it for the next click. it wil only remove children at depth 2 or higher

if you want to remove all the created children at once, you could instead use a 'for loop' like this:
for ( i ; i >1; i--) { removeChildAt(i) }

this starts at depth i, and removes the child at that depth, it also automatically decreases the value of i, and keeps removing the children until i is equal to 1, when it stops.

i hope that helps. to learn more you can read up on :
removeChild
removeChildAt
if conditional statements and
for loops


Return to posts index

Denise Gross
Re: need advice on removing something from the stage
on Oct 12, 2011 at 7:32:48 pm

Yes, thanks so much. That answers my question.

I appreciate your help!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]