A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Code Layout/Design pattern question

  1. #1
    Member
    Join Date
    Apr 2003
    Location
    North East, MD
    Posts
    62

    Code Layout/Design pattern question

    I'm looking for suggestions on the best way to layout my AS code in an application I'm building. It's really a simple web design, but it's the first I've done using pure AS classes/files (none on fla. timeline).

    Currently I have my main document class handling all the basic stuff like the layout of some visual elements, resize handler, etc. What I think I need to do is create a navigator/menu class that handles the layout of menu items (will probably be animated somehow). My question is how/where do I assign the listeners for these menu items to have the main docClass do the right thing? Do I just have the menu class assign eventHandlers to the menu items that call a corresponding parent.menuItemClicked(menuItem) method? Then the parent does the corresponding action?

    Thanks,
    Steve
    "I hate when people put quotes in their signatures" -anonymous

  2. #2
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    the sub class eventHandlers can :
    Code:
    //file SubClass1
    package {
    class {
    public static var AN_EVENT:String = "Some event"
    
    // functions
    
    // in an event function:
    dispatchEvent(new Event = SubClass1.AN_EVENT);
    
    }
    }
    
    
    //and your doc class can have an event listener for 
    someObject.addEventListener(SubClass1.AN_EVENT, docClassFunction);
    
    function docClassFunction(e:Event):void
    {
    // have doc class do something triggered by sub class
    }
    ... hopefully I didn't get the direction you wanted to do things backwards, if I did... DOH

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I think I agree with deadlock32, but I'm not sure if I'm interpreting his code correctly.

    I think the pattern you're looking for is the Command pattern, combined with the Observer pattern.

    It seems like this is the sort of problem that Flex would have a built in widget for, but I'm not familiar enough with it to say they do. There's also ASwing which is an as3 port of the Java Swing classes, which you should be able to use. I encourage you to take advantage of these if they seem like they'll do the job. No point re-inventing the wheel all the time.

    The basic architecture goes like this:
    You have a Menu class which contains a collection of MenuItems. The menu class handles the positioning of the menu items, which render themselves. The Menu class will register an event listener on each of its items for a custom event (ACTIVATED, or SELECTED, or whatever you like), which will contain information about which action should be taken. The MenuItems will have event listeners for MouseEvents such as CLICK, which will dispatch the custom event with the identifier for the command to take.

  4. #4
    Member
    Join Date
    Apr 2003
    Location
    North East, MD
    Posts
    62
    ok, cool. I forgot that I could create my own custom events. Thanks for the tips. I guess this is almost the approach I was taking (minus the custom events).

    Thanks again!
    Steve
    "I hate when people put quotes in their signatures" -anonymous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center