0
votes

I am having trouble embedding a child object to a movieClip with code using AS3 and I am not sure what I am doing wrong.

I am trying to create a new movieclip with code and add it to the stage. I am then trying to embed dynamically created objects inside that movieclip. When I trace the objects it shows that they are on the stage and not embedded into the movieclip. I looked through the forums but I didn't find an answer. Below is a watered down version of the code I have. Any help is appreciated.

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.text.*;
    import flash.net.URLRequest;
    import flash.display.Loader;

    var btnAImage:Loader = new Loader();
    var image:URLRequest = new URLRequest("btn_A.png");

    public function TextWithImage()
    {
        var TextField1:TextField = new TextField;
        var myText1:String = "TEXT FIELD 1";
        var BtnMovieClip:MovieClip = new MovieClip();
        var btnPadding = 5;
        addChild(BtnMovieClip); //add new MC to stage

        btnAImage.load(image);
        BtnMovieClip.addChild(btnAImage); // no error gets thrown. I am trying to add the btnAImage inside of my BtnMovieClip.
        btnAImage.x = btnPadding; //I am able to reference the btnAImage without referencing the BtnMovieClip object.
        btnAImage.y = btnPadding;
        var screenW = stage.stageWidth;

        TextField1.height = 40;
        TextField1.width = 250;

        var textPadding = 5;

        var TextField1_fontFormat:TextFormat = new TextFormat  ;
        TextField1_fontFormat.size = 40;
        TextField1_fontFormat.font = "TestFont";

        TextField1.defaultTextFormat = TextField1_fontFormat;

        BtnMovieClip.addChild(TextField1); // no error gets thrown. I am trying to add the text field inside of my BtnMovieClip.
        TextField1.text = myText1;

        TextField1.x = (btnAImage.width + textPadding);

        TextField1.y = textPadding;
    }
1
Hello and welcome to StackOverflow. Now please explain on the stage and not embedded into the movieclip because word embed refers to font embedding so the problem is unclear.Organis
Perhaps embed is not what I should have written. I am placing objects within a dynamically created movieclip. After doing some research, it appears that I am doing it correctly, however, my issue seems to be that I get a 0 when I check the width of my btnAImage with a trace.trace("btnAImage.width = " + btnAImage.width); I tried to hardcode the width btnAImage.width = 35; but then the image disappears from the stage. Also, when I try to trace it's location my trace tells me that it is on the stage and not in my movie clip. I tried to change my container to a sprite, that didn't fix it.vonnie
Ah. That's because you need to wait till it loads. Initially it is empty and until the image is loaded (it's an asynchronous process so it happens after the initialization code is done) and width/height manipulations on an empty object always goes trouble unless you know exactly what you are doing and how to do it.Organis
Thank you! I should have realized that myself. I did note that when I tried to set the width of BtnMovieClip, it also disappears. It is my empty container. Please note that I changed the code that creates BtnMovieClip from var BtnMovieClip:MovieClip = new MovieClip(); to var BtnContainer:Sprite = new Sprite();vonnie
I figured it out! Thank you all for your comments This is what I did. ` public var btnAImage:Loader; private function LoadImager() { btnAImage = new Loader(); btnAImage.load(new URLRequest("btn_A.png")); btnAImage.contentLoaderInfo.addEventListener(Event.COMPLETE, TextWithImage); } public function TextWithImage(e:Event) { var BtnContainer:Sprite = new Sprite(); addChild(BtnContainer); btnAImage.width = 60; `vonnie

1 Answers

1
votes

I wasn't preloading my image so here's how I fixed it. Thank you to Organis for the help!!!

    public var btnAImage:Loader;
    private function Question8_Answer()
    {
        trace("Question8_Answer() was called");
        btnAImage = new Loader();
        btnAImage.load(new URLRequest("btn_A.png"));
        btnAImage.contentLoaderInfo.addEventListener(Event.COMPLETE, TextWithImage);

    }

public function TextWithImage()
{
    var TextField1:TextField = new TextField;
    var myText1:String = "TEXT FIELD 1";
    var BtnContainer:Sprite = new Sprite();
    var btnPadding = 5;
    addChild(BtnMovieClip);

    BtnMovieClip.addChild(btnAImage); 
    btnAImage.x = btnPadding; 
    btnAImage.y = btnPadding;
    var screenW = stage.stageWidth;

    TextField1.height = 40;
    TextField1.width = 250;

    var textPadding = 5;

    var TextField1_fontFormat:TextFormat = new TextFormat  ;
    TextField1_fontFormat.size = 40;
    TextField1_fontFormat.font = "TestFont";

    TextField1.defaultTextFormat = TextField1_fontFormat;

    BtnMovieClip.addChild(TextField1); // no error gets thrown. I am trying to add the text field inside of my BtnMovieClip.
    TextField1.text = myText1;

    TextField1.x = (btnAImage.width + textPadding);

    TextField1.y = textPadding;


    BtnContainer.width = 300;


}