A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: AS3 - Decorator pattern and loading in from the Library

  1. #1
    Member
    Join Date
    Oct 2003
    Posts
    33

    AS3 - Decorator pattern and loading in from the Library

    Hi,

    I've been following this tutorial:

    http://www.as3dp.com/2009/04/26/acti...esign-pattern/

    And so far everything is working. However, I want my Concrete class to load in an item from the library (basically the graphic for that item). This works fine when just loading in the Concrete class, but as soon as I add a decorator the graphic disappears, and I'm not sure why.

    The code so far is:

    My Abstract Class
    Code:
    package todd.flashBook.navigation{
       
        import flash.display.MovieClip;
       
        public class navButton extends MovieClip{
           
            public function navButton():void{
                trace("Nav button Created");
            }
           
        }
       
    }
    Abstract Decorator
    Code:
    package todd.flashBook.navigation{
       
        public class navButtonDecorator extends navButton{
           
            protected var button:navButton;
           
        }
       
    }
    Concrete Class
    Code:
    package todd.flashBook.navigation{
       
        public class prevButton extends navButton{
           
            public function prevButton():void{
                trace("I am a previous button");
            }
           
        }
       
    }
    Concrete Decorator
    Code:
    package todd.flashBook.navigation{
       
        public class pageSkipDecorator extends navButtonDecorator{
           
            public function pageSkipDecorator(buttonRef:navButton){
                this.button = buttonRef;
            }
           
            public function skipToPage(pageNum:Number){
               
                trace("skipping to page");
               
            }
           
        }
       
    }
    Everything works, I can call functions from each, just the graphic goes.

    I'm calling the Class like this:

    Code:
    var button = new pageSkipDecorator(new prevButton());
    I'm pretty sure I understand how the Decorator pattern works (bar the reference to the component in the decorator abstract class), but I'm not sure why the graphic isn't showing. I get no errors either. This seems like a usefl pattern, and I can already think of ways I can use it in future projects, but not without this stumbling block

    Cheers if anyone can shed some light!

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    You need to create an instance of the ConcreteComponent first, which is your button and then use addChild
    var button:navButton = new prevButton();
    addChild (button);
    button = new pageSkipDecorator(button);
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Member
    Join Date
    Oct 2003
    Posts
    33
    Amazing, thank you!

    Another quick follow up question, when I then trace the width of the button, it comes up 0. Any idea why?

    Code:
    var button:navButton = new prevButton();
    addChild (button);
    button = new pageSkipDecorator(button);
    trace(button.width + "  " + button.height);    // 0 0

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    The reason is probably that button now refers to pageSkipDecorator and that is not a Displayobject.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Member
    Join Date
    Oct 2003
    Posts
    33
    god of course, that was a bit stupid of me.

    I've made the reference to navButton in the Decorator Abstract class public, so I can access it from outside, and it works now with button.button.

    Thank you, your help has been amazing

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