actionscript 3 - remove this child and add new child AS3 -
i've been stuck on quite while, i'm working main.as , livrmscreen.as , livrmscreen.as game screen other movieclip actions going on... have button on livrmscreen wish remove livrmscreen , show homescreen (all homescreen functions in main.as)
var homescreen: homescreen; public function livrmscreen() { backhomebtn.addeventlistener(mouseevent.click, onbackhomebtnclicked); } function onbackhomebtnclicked(evt:mouseevent) { homescreen = new homescreen(); stage.addchild(homescreen); parent.removechild(this); }
this have right now, added parent.removechild because won't remove when removechild ... , because of added stage.addchild home screen show properly.
but when homescreen shows button's don't work ... showing dead movieclip. why that???
i tried put onbackhomebtnclicked function in main.as thinking homescreen functions there , maybe buttons work... in case cant screens remove , add properly
there's flaw in code logics. don't want go deep oop principles or tell use frameworks takes care of these kind of things.
here's cheap , fast way out:
in main.as
private function gotolivrscreen(e:event = null):void { var livrscreen:livrscreen = new livrscreen(); livrscreen.addeventlistener("onclose", onlivrscreenclose); addchild(livrscreen); } private function onlivrscreenclose(e:event = null):void { removechild(e.currenttarget displayobject); // add code show main menu or whatever wanted }
in livrscreen.as
backhomebtn.addeventlistener(mouseevent.click, onbackhomebtnclicked);
and handler
private function onbackhomebtnclicked(evt:mouseevent):void { // remove listeners object have livrscreen garbage collected // when done dispatch event main.as waiting dispatchevent(new event("onclose")); }
this pretty crude way , there can many improvements, make code little cleaner , hopefully, more scalable in case want add new screen or change implementation.
Comments
Post a Comment