Anu KM
Anu KM

Reputation: 1

Accessing children of a MovieClip, when parent has been added dynamically through AS 3

I am new to AS3 programming (just learning) I am trying to create a FB style menu icon which should animate on hover. The icon itself is a MovieClip and is composed of a Bg MovieClip and 3 Horizontal Line MovieClips. (I have split them so as to animate each clip using AS3)

I am instantiating and adding the Parent to my Stage using ActionScript.

The issue I am facing is that I am unable to access the children Clips, as I have not instantiated them individually (but they appear on the stage). I am getting the following error when I try to access them.

Scene 1, Layer 'actions', Frame 1, Line 25, Column 14 1119: Access of possibly undefined property menuBg_mc through a reference with static type MenuIcon.

I also tried instantiating the children in the Parent Constructor Class and tried to reference them. Got the following error:

Scene 1, Layer 'actions', Frame 1, Line 25, Column 14 1119: Access of possibly undefined property menuBg_mc through a reference with static type MenuIcon.

Can you please guide me on what I have to do and why? Am attaching the AS3 code I used.

Actionscript in Frame 1

import flash.display.MovieClip;
import flash.events.Event;

var menuIcon_mc: MenuIcon = new MenuIcon();

addChild(menuIcon_mc);

menuIcon_mc.addEventListener(MouseEvent.MOUSE_OVER, hoverAnimate);

function getChildrenof(container:DisplayObjectContainer):Array {
    var temp:Array = [];
    var containerChildren:Number = container.numChildren;
    trace(containerChildren);
    for (var i:int = 0; i<containerChildren; i++) {
        temp.push(container.getChildAt(i));
    }
    return temp;
}

function hoverAnimate(event: Event): void {
    menuIcon_mc.menuBg_mc.alpha = 0.5;
}

MenuIcon Constructor Class

package  {
    import flash.events.Event;
    import flash.display.MovieClip;

    public class MenuIcon extends MovieClip {

        public function MenuIcon() {
            var menuBg_mc:MenuBg = new MenuBg();
            var menuLineTop_mc:MenuLines = new MenuLines();
            var menuLineCenter_mc:MenuLines = new MenuLines();
            var menuLineBottom_mc:MenuLines = new MenuLines();
        }

    }

}

Am using Actionscript 3 and the Flash Professional CC (15.0.0.173)

Hi, Özgür Ersil. Thanks. I tried your suggestion and the declaration now seems fine. My problem is that I am not sure how to access the children to modify their properties.

package {
  import flash.events.MouseEvent;
  import flash.display.MovieClip;

  public class MenuIcon extends MovieClip {
    public
    var menuBg_mc: MenuBg;
    public
    var menuLineTop_mc: MenuLines;
    public
    var menuLineCenter_mc: MenuLines;
    public
    var menuLineBottom_mc: MenuLines;

    public
    function MenuIcon() {
      this.buttonMode = true;
      this.addEventListener(MouseEvent.MOUSE_OVER, hoverBtn);
    }
    private
    function hoverBtn(e: MouseEvent): void {
      //Changing Child property
      this.menuBg_mc.alpha = 0.5;
    }
  }
}

Am getting an error when executing the hoverBtn():

TypeError: Error #1009: Cannot access a property or method of a null object reference. at MenuIcon/hoverBtn()

I also tried changing the Base Class of the Children to the MenuIcon class. But that too doesn't seem to work.

Am not clear as to why this is happening. If there is a parent clip with multiple children, should the children be individually instantiated or will instantiating the Parent clip is enough? If not, how do I go about doing it?

Upvotes: 0

Views: 157

Answers (1)

&#214;zg&#252;r Ersil
&#214;zg&#252;r Ersil

Reputation: 7013

it is because your menuBg_mc defined as a function variable,not a public variable. MenuIcon class should look like this because your movieclips should be public variable that you can reach them from other classes.

package  {
    import flash.events.Event;
    import flash.display.MovieClip;

    public class MenuIcon extends MovieClip {

       public var menuBg_mc:MenuBg;
       public var menuLineTop_mc:MenuLines;
       public var menuLineCenter_mc:MenuLines;
       public var menuLineBottom_mc:MenuLines;

       public function MenuIcon() {
          menuBg_mc = new MenuBg();
          menuLineTop_mc = new MenuLines();
          menuLineCenter_mc = new MenuLines();
          menuLineBottom_mc = new MenuLines();
       }
    }
}

Upvotes: 1

Related Questions