开发者

How to best manage multi-frame MovieClips with classes?

开发者 https://www.devze.com 2022-12-29 22:08 出处:网络
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

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 w开发者_如何学Crite 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.


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


You can create MovieClips with the elements of each section of your application just like debu suggested, and create a Class for each one so you can addChild / removeChild anytime you want (as long you declare objects for those classes in the main Class)

I'm using such way, and i don't feel any dirty, and as you realize, the first frame will contain only what you want to be there, so you instantiate the other sections and listen to the ADDED_TO_STAGE event on the constructor method to procced.

0

精彩评论

暂无评论...
验证码 换一张
取 消