all groups > flash actionscript > december 2005 >
You're in the

flash actionscript

group:

Best practice


Best practice Don 1000
12/11/2005 10:24:44 PM
flash actionscript:
I have read a few times that it is best practice to put all your actionscript
in the first frame of the main timeline. Now that I have Flash 8 and 'learning
actionscript 2', I'd like to get to grips with actionscript properly, and this
is one area that I'm having problems with.
1. this appears to contradict the rule that actionscript will only work on
objects that exist in the same frame that the actionscript is in. e.g., in the
following simple movie I have a movieclip in frame one of a swf movie. The mc
contains a simple tween, a play button and a return button. Hit the play button
and the tween plays. When it reaches the end the mc stops. Hit the return
button and the mc returns to frame 1 and stops.
The script is in frame 1 of the main timeline.
I can only get it to work if the play and return buttons exist throughout the
entire movie. If the play button only exists in frame 1, and the return button
only exists in the last frame of the mc, then it doesn't work. To make it work
I have to put the action script for each button in the same frame of the actual
mc that each button exists in.
I assume I'm missing something simple, but with bigger, more complex movies I
have spent hours trying to get things to work by writing script in frame 1 of
the maintimeline, only to give up and put the script in the same frame as the
object i am targeting.

movieClip_mc.stop();
movieClip_mc.playButton_btn.onRelease = function(){
movieClip_mc.play();
};
movieClip_mc.returnButton_btn.onRelease = function() {
movieClip_mc.gotoAndStop(1);
};


2. If you import swiffs into a main movie, should the script for the imported
movie be in frame 1 of the main movie, or is it acceptable to keep it within
the imported movie?

So, should I be aiming to write every single scrap of action script in frame1
of the main timeline and if so how do I get around the problem in 1. above?

Thanks a lot for any help on this one.
Re: Best practice kglad
12/11/2005 10:51:36 PM
you want your script to be easy to find, expand and debug. that doesn't mean
it should all be on one frame. but it does mean it shouldn't be attached to
the timelines of 12 movieclips in any of 3 or 4 frames in each movieclip making
it very difficult to find the code you need to find when re-working a project 1
year after its (initial) completion.

you can't define actionscript for an object that doesn't exist. so, writing
script in frame 1 of the _root timeline when the object is instantiated in
frame 10 is not going to work.

to resolve, you must instantiate that object in frame 1 of the _root timeline
(usually making its _visible property 0) and then you can include script
affecting that object in frame 1, or you must put the code in a frame that
plays no sooner than the frame that instantiates the object.

2. it's desirable to keep code that affects a loaded swf entirely within that
external swf.
Re: Best practice Joris
12/12/2005 12:47:48 AM
1.
For the exemple you gave, i think it's more simple to put action directly on
button.
on(release) {goto...;}

The fact is when flash player reads your first frame (action frame), your
object "movieClip_mc.returnButton_btn" does not exists for flash player, so
he can't assign an action to it.

I think the only thing which is important is that your code is clean and
that you know where you did put it. For exemple you can put all your
parameters on an action frame, all your functions in an other one...


Re: Best practice Don 1000
12/12/2005 9:17:22 AM
Thanks for your reply,

So ideally you need to have all your objects instantiated in frame 1 of the
_root timeline and make them invisible/visible as and when you need them?

If you have mc's nested within others then you will have to write the
actionscript for objects within that mc, in frame 1, and have the objects
instantiated in frame 1 as well?

Sorry, I know this must be very basic stuff for you guys and gals, but I just
want to get this clear in my head before I start building a new website.

Thanks a lot.
Re: Best practice kglad
12/12/2005 3:29:30 PM
it's not necessary to put all your code in one frame. but yes, if you were
going to put all your code in frame 1 of your main timeline, you would need all
objects referenced in frame 1 to exist in frame 1.
Re: Best practice Don 1000
12/13/2005 12:37:49 AM
Thanks k,

Re: Best practice kglad
12/13/2005 6:47:29 AM
AddThis Social Bookmark Button