Reputation: 103
I have some MovieClips added and animated on the Flash timeline by an animator. Now I need to attach a script and perform some tasks on these MovieClips in every frame.
The problem is that in the first frame that a MovieClip appears in the timeline I get a null object reference.
Here you have a fla file that demonstrates de problem. On the trace on the output you will see that in frame number 9 the currentObject is null. However, it exists on the timeline.
I'm pretty new to Flash development so any kind of help would be appreciated.
This is the demo script:
processFrame(null)
addEventListener(Event.ENTER_FRAME, processFrame);
function processFrame(e:Event){
trace("frame: ", this.currentFrame);
for (var i:uint = 0; i < this.numChildren; i++) {
var currentObject = this.getChildAt(i);
trace("currentObject: ", currentObject);
}
trace(" ");
if(this.currentFrame == totalFrames) {
removeEventListener(Event.ENTER_FRAME, processFrame);
this.stop();
}
}
Thanks in advance.
Upvotes: 1
Views: 1779
Reputation: 86
Instead of ENTER_FRAME or EXIT_FRAME, try Event.FRAME_CONSTRUCTED
As it's fired after the frame is entered and all the stage is loaded, but before you exit to the next frame. Then you can apply any changes you want to the current frame.
In addition, I don't think you'll need the very first processFrame(null) if you use this Event, as you never get an ENTER_FRAME on frame 1, but you should get a FRAME_CONSTRUCTED.
Upvotes: 2
Reputation: 58152
If you change it to Event.EXIT_FRAME it will work fine, see below:
addEventListener(Event.EXIT_FRAME, processFrame, false, 0, true);
function processFrame(e:Event){
trace("frame: ", this.currentFrame);
for (var i:uint = 0; i < this.numChildren; i++) {
var currentObject = this.getChildAt(i);
trace("currentObject: ", currentObject);
}
if(this.currentFrame == totalFrames) {
removeEventListener(Event.EXIT_FRAME, processFrame);
this.stop();
}
}
Remember to change it in the remove event listener as well ;)
Upvotes: 1
Reputation: 255
Try to replace your fla code above with this :
addEventListener(Event.ENTER_FRAME, processFrame);
function processFrame(e:Event){
trace("frame: ", this.currentFrame);
var currentObject = this.getChildAt(0);
trace("currentObject: " + currentObject + "\n");
if(this.currentFrame == totalFrames) {
removeEventListener(Event.ENTER_FRAME, processFrame);
this.stop();
}
}
You will see the output is exactly the same.
Indeed, you don't need to loop through stage's childs, actually there is always only one child in your fla's clip (at index 0). The problem I think is, when you enter the second part in frame 9, the object is not yet fully loaded/instantiated. I think there is an event to wait for it, but I don't remember exactly.
Upvotes: 0
Reputation: 2677
Does the object on your timeline have the same instancename at every keyframe? That's a common mistake.
Always give your movieclips an instance name in the property panel before adding keyframes on the timeline that manipulate that movieclip.
Upvotes: 0