Search code examples
actionscript-3classbuttonmovieclip

How to use same instance names in 2 different classes as3


Here's the code

Document class:

menu.more.addEventListener(MouseEvent.CLICK, More_func)
function More_func (e:MouseEvent):void
{
    showmore.visible = true;
}

menu is the instance name of the movieclip in the document class. more is the instance name of a button inside the movieclip. And showmore is the instance name of another movieclip.

I want to transfer this code into the menu class. I just simkply copy/paste. But i recieve a message saying: 'Access of undefined property showmore.'.

So i am asking how i can use external object (in this case showmore) into another movieclip class (in this case menu).

The code works fine if it's in the document class.


Solution

  • Sounds like you just needs to go up to the parent for showmore. if you copy this code into the menu class, then referencing showmore looks for it in the menu class.

    You need to be able to reference your document class from the menu class. You could do this a few different ways:

    1. (if menu and showmore are both children of the document class on the display list and showmore is public object) You'll need to cast the parent property or you'll get a compile error.

      DocumentClassName(parent).showmore.visible = false;

    2. Create a static variable on document class you can tap into IN YOUR DOCUMENT CLASS:

      public static var me:DocumentClassName;

    IN YOUR DOCUMENT CLASS CONSTRUCTOR:

    me = this;
    

    IN YOUR MENU CLASS More_func:

    DocumentClassName.me.showmore.visible = false;