简体   繁体   中英

fetch variables from child movieclip in parent? Flash AS3

I have some variables defined in a child MC:

var first_nme = data.return_first;
    var second_nme = data.return_second;
    var email_addr = data.return_email;
    var user_domain = data.return_domain;
    var user_name = data.return_username;

I'm trying to use those variables in a parent movieclip/another movieclip.

I've tried the trace method:

    trace(MovieClip(verif).first_nme);

//'verif' is the mc name

    trace(MovieClip(parent).first_nme);

both of the above return an undefined error for the var first_nme.

could anyone tell me how to do this properly?

thanks

EDIT:

Child:

function completeHandler(event:Event):void{

    // Load the response from the PHP file
    var data:URLVariables = new URLVariables(event.target.data);
    var returnn_stat = data.return_status;
    var ffferror256 = data.return_value;
    returnn_stat = returnn_stat.replace(/\s/m, "");


    if (returnn_stat == "FAILED"){
    status_txt.text = ffferror256;

    }
    else if (returnn_stat == "PASSED"){

    var first_nme = data.return_first;
    var second_nme = data.return_second;
    var email_addr = data.return_email;
    var user_domain = data.return_domain;
    var user_name = data.return_username;

    MovieClip(parent).gotoAndPlay("confirm");

    first_txt.text = first_nme;
    second_txt.text = second_nme;
    email_txt.text =  email_addr;
    username_txt.text = user_name;
    domain_txt.text = user_domain;


    }
    else {
        status_txt.text = "Oops, something went wrong. Please try again later.";

    }

}

PARENT:

trace(MovieClip(verif).getChildAt(0).first_nme);
txtt_txt.text = first_nme;

I've tried with the frame number in the line trace(MovieClip(verif).getChildAt(0).first_nme); but still the same error.

尝试将first_nme设为公开范围:

public var first_nme = data.return_first;

It seems you are missing the child reference
Assuming you set the name of the child to "childname"

childmc.name = "childname"; // to set the name of the child
...
trace(MovieClip(verif).getChildByName("childname").first_nme);

OR

trace(MovieClip(verif).getChildAt(0).first_nme);
trace(MovieClip(verif).getChildAt(1).first_nme);
etc...

it seems that you're calling the trace to early because you load something asynchronously in the child mc. the right thing to do here would be to load the stuff in the child mc and when the load process is complete ( onCompleteHandler ) you fire an Event that is handled by the parent mc. you could also add the loaded data to the (custom-)event and pass it to the parent using the eventobject:

function completeHandler(event:Event):void 
{
    // Load the response from the PHP file
    var data:URLVariables = new URLVariables(event.target.data);
    var returnn_stat = data.return_status;
    var ffferror256 = data.return_value;
    returnn_stat = returnn_stat.replace(/\s/m, "");


    if (returnn_stat == "FAILED")
    {
        status_txt.text = ffferror256;
    }
    else if (returnn_stat == "PASSED")
    {
        var evt:MyCustomEvent = new MyCustomEvent(MyCustomEvent.DATA_LOADED, true, true);
        evt.first_name = first_nme;
        evt.second_name = second_nme;
        evt.email =  email_addr;
        evt.username = user_name;
        evt.domain = user_domain;

        dispatchEvent(evt);
    }
    else
    {
        status_txt.text = "Oops, something went wrong. Please try again later.";
    }
}

and then you have an eventlistener in your parent mc:

this.addEventListener(MyCustomEvent.DATA_LOADED, onDataComplete);
// ...
private function onDataComplete(evt:MyCustomEvent):void
{
    gotoAndPlay("confirm");

    first_txt.text = evt.first_name;
    second_txt.text = evt.second_name;
    email_txt.text =  evt.email;
    username_txt.text = evt.username;
    domain_txt.text = evt.domain;
}

and last but not least the custom event class:

package 
{
    import flash.events.Event;

    public class MyCustomEvent extends Event
    {
        public static const DATA_LOADED:String = "onDataLoaded";

        public var first_name:String;
        public var second_name:String;
        public var email:String;
        public var username:String;
        public var domain:String;

        public function MyCustomEvent($type:String, $bubbles:Boolean = false, $cancelable:Boolean = false)
        {
            super($type, $bubbles, $cancelable);
        }
    }
}

See this link

define the variables as public in the child mc, otherwise they will be scoped to the default visibility, ie not visible to another component unless both accessing and accessed components belong to the same namespace

Few things come to mind.

  1. Just to be sure: I see you use a MovieClip(parent).gotoAndPlay ; the child movieclip is still present on the stage at that part of the timeline in the parent? Else it will be removed, and getChildAt() won't return it.

  2. You are sure the getChildAt(0) returns the correct MovieClip? Since shapes, graphics, etc. are also children (you can check the numChildren property of the parent).

  3. getChildAt() returns a result as DisplayObject type; which is not a dynamic class. Trying to reference any variable or property that is not known will result in an error. However if you typecast the result to a MovieClip; you can access new variables and properties (since MovieClip is a dynamic class). So try the following:

     trace(MovieClip(MovieClip(verif).getChildAt(0)).first_nme); 

BTW: if you have given the clip a instance name, you can also reference it by the name instead of using getChildAt() (verif.somename.first_nme).

There is no way the parent clip can access those variables unless you declare them public. If you do not declare a property attribute it will default to internal meaning it will only be visible to classes in the same package. You will not get any warnings when you try and access them as MovieClips are dynamic, however when you try and access them you will receive an error.

Would be helpful if you posted the whole class.

Have you set instance name for your child mc?

if yes than try to follow this.

trace(MovieClip(root).child_mc.variable_name);

another important thing you should remember that whenever you create child MovieClips it will be executed after its parent so if you are tracing variable (of child) directly from parent. it will displays NAN as because it is not executed.

so that to trace child Mc's variable from parent just call a function (of parent that trace child mc's variable) in your child.

code in your child mc

var test:Number = 123;
MovieClip(root).test_var();

code in your root frame

function test_var()
{
    trace(MovieClip(root).mc_test.test);
}

you need to create variable before function.

    var first_nme:String;
    var second_nme:String;
    var email_addr:String;
    var user_domain:String;
    var user_name:String;

function completeHandler(event:Event):void{

    // Load the response from the PHP file
    var data:URLVariables = new URLVariables(event.target.data);
    var returnn_stat = data.return_status;
    var ffferror256 = data.return_value;
    returnn_stat = returnn_stat.replace(/\s/m, "");


    if (returnn_stat == "FAILED"){
    status_txt.text = ffferror256;

    }
    else if (returnn_stat == "PASSED"){

    first_nme = data.return_first;
    second_nme = data.return_second;
    email_addr = data.return_email;
    user_domain = data.return_domain;
    user_name = data.return_username;

    MovieClip(parent).gotoAndPlay("confirm");

    first_txt.text = first_nme;
    second_txt.text = second_nme;
    email_txt.text =  email_addr;
    username_txt.text = user_name;
    domain_txt.text = user_domain;


    }
    else {
        status_txt.text = "Oops, something went wrong. Please try again later.";

    }

hope it helps........

You're variables aren't declared at the right scope level. You're declaring them inside of a function, which makes them invisible to the parent class of the movieclip. If you want to be able to access the variables from the parent class, you have to declare them directly in the child class, so they are included when the child is instantiated.

For example:

package
{

   import flash.display.MovieClip;

   public class WHATEVER extends MovieClip
   {
       // declare your variables here
       public var SOMEVAR:String;

       public function WHATEVER():void { }

       private function onComplete(e:Event):void
       {
            SOMEVAR = "Something";

            // this variable will not be visible to parent
            var NEWVAR:String = "I'm invisible";
       }
   }
}

Since that value is being filled asynchronously from when it is defined, you need to test the value before using it in the parent. Alternatively, you can trigger an event when the value is set, and have the parent listen for that event to update what it needs.

I am guessing that the child movieclip is sitting on the timeline and you are trying to access the variables inside it when the play head reaches that frame in the parent timeline. If that's indeed the case then the issue is happening because you are trying to access the content of an object before it has been rendered.

Try putting your variable access code inside a function and call it using an Event.Render event listener. Then all you need to do is make a call to stage.invalidate() from the timeline and the variable should be accessible.

For example:

On the parent timeline:

this.addEventListener(Event.RENDER, stageRender);

function stageRender(e:Event):void 
{
    //your variables access code goes here
}

stage.invalidate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM