views:

215

answers:

1

When we work with buttons we set up our listener and the function it uses, but if we have the button animate in or show up elsewhere in a later frame, we will get an error saying the object doesn't exist.

What would be the more common or correct approach, set up our functions on frame 1 then attach the listener on the frame the button comes in? or keep our button always there just not visible until the needed frame? or is there another approach?

+2  A: 

Thats correct. If you are only creating the Button in another frame later then it does not exist and you will get the error.

Best practise is to keep all your code together in one central place. As soon as you start putting ActionScript in different frames it becomes harder to manage and maintain.

Setting up your functions in frame 1 and then hiding/showing or enabling/disabling is better than adding listeners in different frames. This way you know that all the code is in frame 1.

I personally do not like using frames at all as if you have code in the first frame, and then you play the clip, when you play it again you will lose any local data that you had (from the sounds of it this is not so much of a problem for this project). I have seen first hand the mess (others not myself) caused by spreading code through the timeline.

My recommendation is to always just have 1 frame and have layers which you hide and show as necessary. If you have some animation put it in a MovieClip and then place it on the stage in frame 1.

Allan