PDA

View Full Version : Making a generated movieclip self destruct (remove from screen) after x seconds?



Gemixin
July 7th, 2009, 08:54 AM
I'm making a game where little parcels appear and if they aren't dragged to the correct zone within 5 seconds, they disappear and the player loses points.

The problem i'm having is that i'm generating the movieclip parcel objects as follows:

function generateMCByString(id:String):MovieClip
{
var mcObj:Object = null;
mcObj = getDefinitionByName(id.toString());
return (new mcObj()) as MovieClip;
}

Then creating the movieclip and adding it to the screen depending on certain conditions as follows:

mc = generateMCByString(currentParcel);
mc.value = currentParcel;
addChild(mc);
mc.x = 386.9;
mc.y = -100;

mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStarter);
mc.addEventListener(MouseEvent.MOUSE_UP, dragStopper);

tweenMove0 = new Tween(mc, "y", None.easeOut, -100, 238, 3, true);

currentParcel is 1 of 3 (randomly selected) movieclip names (redParcel, yellowParcel and orangeParcel).

What I need to do is add some code to say "after a movieclip has been generated...if it is still here in 5 seconds time..."

It sounds simple but I can't figure it out for the life of me! I've tried setInterval with something like:

timedProcess = setInterval(timeCheck, 5000);

but how can I pass the actual movieclip "handle" along with the function so that timeCheck knows which movieclip to delete?

Any suggestions gratefully received!

Thanks

marspark
July 7th, 2009, 10:46 AM
I'm making a game where little parcels appear and if they aren't dragged to the correct zone within 5 seconds, they disappear and the player loses points.

The problem i'm having is that i'm generating the movieclip parcel objects as follows:

function generateMCByString(id:String):MovieClip
{
var mcObj:Object = null;
mcObj = getDefinitionByName(id.toString());
return (new mcObj()) as MovieClip;
}

Then creating the movieclip and adding it to the screen depending on certain conditions as follows:

mc = generateMCByString(currentParcel);
mc.value = currentParcel;
addChild(mc);
mc.x = 386.9;
mc.y = -100;

mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStarter);
mc.addEventListener(MouseEvent.MOUSE_UP, dragStopper);

tweenMove0 = new Tween(mc, "y", None.easeOut, -100, 238, 3, true);

currentParcel is 1 of 3 (randomly selected) movieclip names (redParcel, yellowParcel and orangeParcel).

What I need to do is add some code to say "after a movieclip has been generated...if it is still here in 5 seconds time..."

It sounds simple but I can't figure it out for the life of me! I've tried setInterval with something like:

timedProcess = setInterval(timeCheck, 5000);

but how can I pass the actual movieclip "handle" along with the function so that timeCheck knows which movieclip to delete?

Any suggestions gratefully received!

Thanks

You can use the timer class:



var myTimer:Timer = new Timer(5000, 1);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();

function timerHandler(event:TimerEvent):void {
//remove movieclip
}

For full reference, check here:

http://www.hexosearch.com/se/external.aspx?zqz=timer&zlvz=0&zvtz=1&zurlz=http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/Timer.html&ztz=flash.utils.Timer+%28ActionScript+3.0%29

Gemixin
July 7th, 2009, 10:50 AM
Thanks marspark but the problem with that is how does the timerHandler know which movieclip to remove? There would be several parcels on screen at that point, so how can I tell the timerHandler to only remove the movieClip that triggered the timer?

marspark
July 7th, 2009, 11:00 AM
The best practice i would say is to create a custom parcel class, and put a timer inside the constructor.

Gemixin
July 7th, 2009, 11:13 AM
That's a great idea - I've not actually got in to all that yet with AS3 and i'm still using code within the layers of the movie itself rather than an external AS file. I will look in to that but it makes perfect sense and would probably simplify a lot of the other code I've got too.

Thanks very much :)