How to access dynamic text field on stage from wit

2019-08-24 02:32发布

问题:

I'm trying to access a text input that I've already placed on the stage (inside a movie clip) but with no luck.

I've defined an instance name for this dynamic text field which is currentUserCount

I've got something like this set up in the document class actionscript file:

package {

    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;

    public class myProject extends Sprite {

        public function myProject() {
            // Trying stuff like
            trace(currentUserCount);
            trace(movieClipName.currentUserCount);
            trace(root.currentUserCount);
        }
    }
}

What am I missing?

When I run this I get:

1120: Access of undefined property currentUserCount.
1120: Access of undefined property movieClipName.
1119: Access of possibly undefined property movieClipName through a reference with static type flash.display:DisplayObject.
1120: Access of undefined property currentUserCount.

回答1:

If all of the movieclips on the stage had the same nested clip inside of it, you could also just reference the inner clips like so:

for (var i=0; i<stage.numChildren; i++){
  var mc = stage.getChildAt(i)
  mc.subClip.play()
}


回答2:

Make sure the MovieClip and the TextField have instance names in your .fla file.

// MovieClip instance name: mc;
// Textfield instance name: tf;

Then in your document class:

package {

    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;

    public class myProject extends Sprite {

        public function myProject() {
            mc.tf.text = "Text you wanna see";
            trace(mc.tf); // [Object TextField]
        }
    }
}


回答3:

My problem was that I was referencing a movie clip within a movie clip and I didn't know I had to create an instance name for each one and work my way in code through each one.

I thought the instance names were a part of a global namespace.

So I was doing something like:

myMovieClip.play();

when I should have been doing:

mainMovieClip.subMovieClip.myMovieClip.play();