actionscript 3 - ActionScript3 Function to hide or show movieclip currently on stage in a different class -
hello lovely actionscript peoples. having bit of problem creating button using as3 can hide or show specific movieclip being called different class. think, honestly, problem don't fundamentally understand oop / classes utilized in as3, post understanding of problem here -
i have 3 different classes,
consstartmenu.as
(document class), menu.as (the menu want hide , show), ,sidemenu.as
(contains button want hit hide / show menu). calling display of menu , sidemenu movieclipsconsstartmenu.as
. relevant code here -public var menudisplay:menu = new menu(); public var sidemenudisplay:sidemenu = new sidemenu(); public function consstartmenu():void { stage.addchild(menudisplay); menudisplay.x=stage.stagewidth * .5; menudisplay.y=stage.stageheight * .5; stage.addchild(sidemenudisplay); sidemenudisplay.x=stage.stagewidth*.98; sidemenudisplay.y=stage.stageheight*.5; }
within
sidemenu.as
, have code reference menu think? on stage -public var centermenu:menu = new menu();
and on actions of frame button on have -
import flash.events.mouseevent; stop(); opensidemenu.addeventlistener(mouseevent.click, linkopensidemenu); function linkopensidemenu(event:mouseevent):void { if (centermenu.parent.stage){ trace ("center menu in display list"); centermenu.parent.removechild(centermenu); } else { trace("adding menu stage"); centermenu.parent.addchild(centermenu); } }
when click button error -
typeerror: error #1009: cannot access property or method of null object reference. @ sidemenu/linkopensidemenu()[sidemenu::frame1:9]
any appreciated.
your issue confusion between object references , instantiation.
when use new
keyword, creating whole new object.
so in constartmenu
when this:
public var menudisplay:menu = new menu();
you creating new menu
object.
and, in sidemenu
when this:
public var centermenu:menu = new menu();
you creating second menu
object (that has nothing first).
what want, reference first menu created.
there few ways can this:
pass reference in constructor of
sidemenu
.public var menudisplay:menu; public var sidemenudisplay:sidemenu; public function consstartmenu():void { menudisplay = new menu(); sidemenudisplay:sidemenu = new sidemenu(menudisplay); //...rest of code
//then in side menu class: public var centermenu:menu; public function sidemenu(menu:menu){ centermenu = menu; //now have reference menu }
make
menudisplay
var inconstartmenu
static
. (static variables accessable through class , not part of instantiationpublic static var menudisplay:menu = new menu(); //now can access anywhere in app doing: constartmenu.menudisplay
use parentage find it. since
constartmenu
instance parent ofsidemenu
, can access public methods/var ofconstartmenu
using parent keyword://from within sidemenu parent.menudisplay; //or, avoid warning, tell compiler parent of type constartmenu (cast) constartmenu(parent).menudisplay
Comments
Post a Comment