flashactionscript-3addchild

Actionscript 3 addChild Issue


This is a test for a XML image menu. In the code I create my containers, I have a movie clip in the stage (menu_mc).

CODE:

import flash.display.MovieClip;
import fl.text.TLFTextField;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.formats.TextAlign;
import flash.display.Sprite;

var title_mc:TLFTextField;
var img_mc:MovieClip;
var bg_mc:MovieClip;
var myFormat:TextLayoutFormat;
var myTextFlow:TextFlow;

createContainers();

function createContainers():void {

//CREATE IMAGE CONTAINER
img_mc = new MovieClip();
menu_mc.addChild(img_mc);
img_mc.x = -50;
img_mc.y = -60,10;

//CREATE TEXTFIELD
title_mc = new TLFTextField();
addChild(title_mc);
title_mc.x = 0;
title_mc.y = 20;
title_mc.width = 120;
title_mc.height = 50,70;
title_mc.multiline = true;
title_mc.embedFonts = true;
title_mc.antiAliasType = AntiAliasType.ADVANCED;
title_mc.wordWrap = true;
title_mc.text = "text text text text text text text";

myFormat = new TextLayoutFormat();
myFormat.textIndent = 0;
myFormat.color = 0x0000FF;
myFormat.fontFamily = "Arial";
myFormat.fontSize = 20;
myFormat.textAlign = TextAlign.CENTER;
myFormat.textAlignLast = TextAlign.CENTER;

myTextFlow = title_mc.textFlow;
myTextFlow.hostFormat = myFormat;
myTextFlow.flowComposer.updateAllControllers();

//CREATE BACKGROUND CONTAINER
bg_mc = new MovieClip();
menu_mc.addChild(bg_mc);
bg_mc.x = 0;
bg_mc.y = 70;
bg_mc.width = 120;
bg_mc.height = 50;

//CRETAE SPRITE
var square:Sprite = new Sprite();
square.graphics.lineStyle(1,0x000000);
square.graphics.beginFill(0x000000);
square.graphics.drawRect(0,0,300,300);
square.graphics.endFill();
menu_mc.addChild(square);

}

My problem is I want that, for example, bg_mc inside menu_mc and square inside bg_mc.

If I try, for example:

menu_mc.bg_mc.addChild(square); //error

or

menu_mc.addChild(title_mc);  //textfield disapears

Flash give me this error:

TypeError: Error #1010: A term is undefined and has no properties

Why?


Solution

  • If you want bg_mc inside menu_mc do : menu_mc.addChild(bg_mc) , and then square inside bg_mc do : bg_mc.addChild(square).

    menu_mc.bg_mc.addChild(square); will give you an error because in the MovieClip menu_mc there is no property named bg_mc. adding a child XYZ don't imply the creation of a property named XYZ.