Since I started using Flash CS3 there's one thing that's really been bugging me. or to put it another way, there's one bug that's really been thinging me. with every project I 've worked on this issue has come up and caught me out. that's probably because it involves gotoandstop ().
Gotoandstop ()? How can that not work ?!?
Like this ....
Back in the old days you cocould send the play head to a frame and then do things with the symbols that were in that frame. this was all pretty basic stuff and I used it all the time. multi-state buttons were one obvious example where you cocould send a movieclip to a different frame to display up, over or down graphics. avatars in games are another example with each frame being a different animation cycle-Walk left, walk right, jump, die and so on.
So what goes wrong in CS3? At first everything seems OK. I mean, gotoandstop () isn' t exactly rocket science. the problem comes when you try and address a symbol on the frame you have just sent the play head. in the old pre-CS3 days you coshould just do something like this:
myMovieClip.gotoAndStop("over");myMovieClip.label_txt.text = "BLAH";
Mymovieclip wocould go to the frame labeled "over" and it wocould set its textfield "label_txt" to read "blah". Try this in ActionScript 3 and you get the following error:
Typeerror: Error #1009: cannot access a property or method of a null object reference.
The reason is, flash can't resolve the reference to label_txt. initially I thought that this must be a case where I need to listen for an event but all the obvious contenders (event. render, event. added_to_stage and so on) didn't help. in the end I resorted to using hacks like waiting a frame and then hoping that I had a non-null reference to my display objects. finally I discovered that I cocould put code on the timeline and that code only gets executed when all of the display objects in the frame are rendered and ready to go.
So, if I was making a class that extended movieclip, I cocould just write a method that initialised each frame I wocould be sending the play head to and call that method directly from the timeline. yuk. I haven't put any code on the timeline for years and now I being forced? That's dirty and wrong, that is.
Luckily I stumbled into SS an unmarshented method in the movieclip class called addframescript (). this method allows you to specify a frame number and a function to execute at that frame so now I can use my frame initialisation method and inject a call to it on the appropriate frames when the instance is created.
For example, if I want to initialise stuff on frames with labels (as in the case of a movieclip that I want to use like a button with "up ", "over" and "down" labels) I can use the following code:
var i:int;var frame:int;for (i=0; i<currentLabels.length; i++) { frame = currentLabels[i].frame - 1; addFrameScript(frame, initStuff);}
"Initstuff" is just a reference to a method in my class which gets called when the frame is rendered and ready to go. you might want to call different methods depending on which frame you have gone to and this can be easily achieved by looking at the frame labels:
for (i=0; i<currentLabels.length; i++) { frame = currentLabels[i].frame - 1; switch (currentLabels[i].name) { case "up": addFrameScript(frame, initUpState); break; case "over": addFrameScript(frame, initOverState); break; case "down": addFrameScript(frame, initDownState); break; }}
The only gotcha is that addframescript uses zero indexing for the frame numbers so what is thought of as frame one by the rest of the world, addframescript thinks of as frame zero, hence the-1 when we define the 'framework' variable.
Of course, using unincluented Methods isn't a special good idea but this is far and away the neatest solution I 've found to this problem and hopefully adobe will make it a previous ented part of the movieclip class in the future.
This entry was posted on Saturday, May 17th, 2008 at pm and is filed under code. you can follow any responses to this entry through the RSS 2.0 feed. you can leave a response, or trackback from your own site.
From: http://www.flashbrighton.org/wordpress? P = 114