Flash/AS3: Accessing the instance of a DisplayObject on a mid-timeline frame
I have the Dynamic Text field that has an instance name on a mid-timeline frame in Flash CS4.
I have a class that inherits from Sprite, and which is always added to the stage on instantiation. How can I get a reference to my Dynamic Text instance from this class?
Thanks!
EDIT: example:
My Main class (linked in Flash's publish settings) goes like this:
protected function beginGame(e:MouseEvent){
gotoAndStop(8);
var game:GameContainer = new GameContainer(41,8);
addChild(game);
game.x=36;
game.y=128;
}
Game Container extends MovieClip, and draws some pretty gfx.
On Frame 8, in the Flash IDE, there is a Dynamic开发者_StackOverflow社区 Text element instance named "scoreText".
I want to access this from the GameContainer class.
Possible?
As long as GameContainer is on the stage you can do:
if (MovieClip(root).scoreText) {
MovieClip(root).scoreText.text = "hello";
}
If it's not on the stage it won't have a root property.
I think you should assign the textfield to a variable. Note you cannot access an object that is on another frame, you can only access it when you are on the frame.
But.. you could try to use addFrameScript() for that.
public var scoreText:TextField
protected function init():void
addFrameScript(8, assignTextfield);
}
public var assignTextfield():void
{
this.scoreText = scoreText;
}
// somewhere in your class
protected function beginGame(e:MouseEvent):void
{
if (this.scoreText) this.scoreText.text = 'hello world';
}
I did not test the code but it should be something like this. If you are compiling with the Flex SDK this assignment is not needed, since it understands it already (strange different btw)
精彩评论