0
votes

I'm trying to make a block of AS3 code work with an older Flash movie. A movie consisting of ~4000 keyframes and having static movie clips added to main timeline periodically.

On frame no 120, a movieclip with instance name of playButtonMC is added to stage. playButtonMC has a button instance named playButton inside it. I want to assign click event handler to the button inside this movie clip from main timeline but I'm failing to get a reference to the movie clip from actions on keyframe # 121 on main timeline.

I've tried following in vain:

var myMc:MovieClip = stage.getChildByName("playButtonMC") as MovieClip;
trace(myMc);
trace(playButtonMC);
trace(root.playButtonMC);
trace(stage.playButtonMC);

// If I get a reference, I plan to do following to attach event listener.
// Please advise if its incorrect as well
myMc.playButton.addEventListener(MouseEvent.CLICK, doStuff);

function doStuff():void{
    trace('called');
}

All calls to trace result in null.

I'm not a Flash developer in any sense but I've been handed over this small task. I know the right AS3 way of doing it must be by dynamically generating the movie clips and storing the references in variables but, at the moment, I'm only trying to make it work somehow.

EDIT

trace(stage.playButtonMC); actually creates an error ReferenceError: Error #1069: Property playButtonMC not found on flash.display.Stage and there is no default value.

2

2 Answers

1
votes

If you have access to the source code of the older movie, the easiest way is to add getters and setters for the instances you need to know about. Looking at your posted code, it looks like you probably do have access to the source, but for some reason you're using timeline code vs. a document Class. If you're going to use timeline code, you might want to just shove it on the frame with the button and don't worry about it any further.

If you don't just watch for ADDED_TO_STAGE (which doesn't bubble, so you'll need to watch in capture phase) and sift through until you find the stuff you care about. You can switch on name or getQualifiedClassName() or some other condition that does what you want.

For a rundown of both techniques, you can read this. Example code here.

1
votes

Are you sure the instance is still present?

stage != root

Either use root or use this in case you have the code on your MainTimeline instance. Definitely not stage.