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 -

  1. 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 movieclips consstartmenu.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; } 
  2. 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:

  1. 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     } 
  1. make menudisplay var in constartmenu static. (static variables accessable through class , not part of instantiation

    public static var menudisplay:menu = new menu();  //now can access anywhere in app doing: constartmenu.menudisplay 
  2. use parentage find it. since constartmenu instance parent of sidemenu, can access public methods/var of constartmenu using parent keyword:

    //from within sidemenu parent.menudisplay;  //or, avoid warning, tell compiler parent of type constartmenu (cast) constartmenu(parent).menudisplay 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -