views:

55

answers:

1

After switching to AS3, I've been having a hell of a time figuring out the best way to manage MovieClips that have UI elements spread across multiple frames with a single class. An example that I am working on now is a simple email form. I have a MovieClip with two frames:

  • the 1st frame has the form elements (text inputs, submit button)
  • the 2nd frame has a "thank you" message and a button to go back to the first frame (to send another email)

In the library I have linked the MovieClip to a custom class (Emailer). My immediate problem is how do I assign a MouseEvent.CLICK event to the button on the 2nd frame? I should note at this point that I am trying to avoid putting code on the timeline (except for stop() calls). This is how I am 'solving' the problem now:

  • Emailer registers an event listener for a frame change ( addEventListener("frame 2", onFrameChange) )
  • On the 2nd frame of the MovieClip I am calling dispatchEvent(new Event("frame 2")); (I would prefer to not have this code on the frame, but I don't know what else to do)

My two complaints with this method are that, first I have calls to addEventListener spread out across different class methods (I would rather have all UI event listeners registered in one method), and second that I have to dispatch those custom "onFrameChange" events. The second complaint grows exponentially for MovieClips that have more than just 2 frames.

My so called solution feels makes me feel dirty and makes my brain hurt. I am looking for any advice on what to do differently. Perhaps there's a design pattern I should be looking at? Should I swallow my pride and write timeline code even though the rest of my application is written in class files (and I abhor the Flash IDE code editor)?

I absolutely LOVE the event system, and have no problem coding applications with it, but I feel like I'm stuck thinking in terms of AS2 when working with mutl-frame movieclips and code. Any and all help would be greatly appreciated.

+2  A: 

I think unfortunately that your best move is to ditch your multi-frame coding style. Having stuff like that on different frames is only going to cause you problems, exponentially increasing the more frames you have, just as you said.

A quick and dirty solution which avoids the time line might be to have the contents of each frame all inside a specific MovieClip, then have all those MovieClips on the first frame of your parent MC. Then when you want to switch between sections, you could just set one of those MovieClips to transparent, and the other to visible. So for your Emailer example, once the user has sent off the email you set the form to invisible, and the thank you screen with the back-button to visible.

In this way, you can just set your Event Listeners for buttons inside what would have been other frames when your class' constructor is called, and not worry about all those Events you were setting up to detect frame changes. Like I said, it's possibly a bit of a dirty solution, it really depends on what you're trying to do.

Hope I haven't misunderstood your question, and that this might be some help :)

debu

debu
Arms
Yeah, that was going to be my other suggestion, though I'm not quite sure how best to advise you for making that solution work in a manageable way. Depending on how many different frames there are with different information, it could get a bit out of control. But depending on what information you're displaying, you might be able to condense many of the frames to just one basic class, which has dynamic text boxes that can be resized and repositioned depending on what content you want to display. What are the more complex examples you have which use several frames?
debu
@debu: I agree as well. Using multiple frames is good for animations, but bad for defining UI's and application flow. Just make the two different forms be different movieclips, and create/destroy (or hide/unhide) as needed.
davr