actionscript 3 - I cant remove child of this movie clip upon clicking the back button to go function goHomePage (evt:Event):void{ -
help. need remove newcontainer(this movie clip) after clicking button go homepage. loads homepage , newcontainer still there. :( did go wrong?
import flash.events.mouseevent; import flash.events.event; import fl.motion.motionevent; import flash.net.urlvariables; import flash.display.movieclip; import flashx.textlayout.elements.configuration; var ctr:int = 0; var now:date = new date(); var myurl:string = "http://localhost:8888/eventspictures/getdata.php"; var scriptloader:urlloader = new urlloader(); var scriptrequest:urlrequest = new urlrequest(); var newcontainer:movieclip; scriptrequest.url = myurl + "?ck=" + now.gettime(); scriptloader.addeventlistener(event.complete, handleloadsuccess); scriptloader.addeventlistener(ioerrorevent.io_error, handleerror); scriptrequest.method = urlrequestmethod.post; scriptloader.load(scriptrequest); function handleloadsuccess(evt:event):void { (var j:number = 0; j <4; j++) { var newcontainer:movieclip = new con(); newcontainer.name = string(ctr); newcontainer.y = j*80 +65; newcontainer.x= 16; stage.addchild(newcontainer); var variables:urlvariables = new urlvariables(evt.target.data); trace(variables.output); var parse:string = variables.output; var parsed:array = parse.split("<|>"); var tab:string = '	'; var eventname:string = ''; var date:string=''; var slotsleft:string=''; // different variable names assign different column names(etc; eventname, date, slotsleft) // loop through.. start o (var i:number = 0; i<parsed.length-1; i++) { trace(parsed[i]); var item:string = parsed[i]; var itemarray:array = item.split(","); eventname += itemarray[2] + tab + "<br>"; date += itemarray[3] + tab; slotsleft += itemarray[4] + tab; trace(eventname); newcontainer.eventname_txt.htmltext = eventname; newcontainer.date_txt.htmltext= date; newcontainer.slotsleft_txt.htmltext=slotsleft; } } //slotsleft_txt.x = 270; } function handleerror(evt:ioerrorevent):void { } backbutton_mc.addeventlistener(mouseevent.click, gohomepage); function gohomepage (evt:event):void{ gotoandplay("dashboard");
removechild(newcontainer); }
stop();
in function handleloadsuccess()
have loop creates 4 movieclip
's , adds them stage.
but in gohomepage()
function try remove 1 object stage. turns out object null
in example (trying remove object null
should generate error if using debug flash player).
the reason null
because have defined 2 variables named newcontainer
in code: 1 declared @ beginning of script, , second 1 inside function handleloadsuccess()
(because using keyword var
on each line).
the variable created inside function exists while function executing. variable created outside function never seems receive value.
two approaches can use:
keep track of things add stage, putting them in array
:
replace newcontainer
variable outside function array
called containers
:
var containers:array = [];
in function handleloadsuccess()
, when add each movieclip
stage, add them containers
array:
stage.addchild(newcontainer); containers.push(newcontainer);
finally in function gohomepage()
iterate on containers
array remove each movieclip
:
for (var j:int = 0; j < containers.length; j++) { stage.removechild( containers[j] ); } // reset containers array it's empty next time // , remove references movieclips (to prevent memory leak) containers=[];
blindly remove stage
if accetpable, easier. iterate backwards on children of stage in gohomepage()
function:
for (var j:int = stage.numchildren; j >= 1; j--) { stage.removechildat(j - 1); }
Comments
Post a Comment