Skip to main content
April 29, 2010
Question

Using Custom Events and bubbleing!

  • April 29, 2010
  • 1 reply
  • 241 views

Hello,

So heres the issue I'm haveing, my event listener is not being triggered from the parent, and not retrieving my string for each button being clicked on.

Here is the event listener which listens for the subclass to dispatch an event called MenuEvent.LOAD_PAGE and then passes a string to a variable thats called whatPage thats defined within my custom event.

page_mc.addEventListener(MenuEvent.LOAD_PAGE, loadPage, false, 0, true);

private function loadPage(e:MenuEvent):void
{
            trace("This page loading is: "+e.whatPage);

}

which my sub class dispatches a event and passes a string of my page being clicked:

var _passPageSelected:String = String(e.target.data);

this.dispatchEvent(new MenuEvent(MenuEvent.LOAD_PAGE, _passPageSelected, true));

Heres my custom event:

package 
{
    import flash.display.*;
    import flash.events.*;
   
    public class MenuEvent extends Event
    {
        public static const LOAD_PAGE:String = "loadPage";
        public var whatPage:String;
        // Initialization:
        public function MenuEvent($type:String, $id:String, $bubbles:Boolean = false, $cancelable:Boolean = false)
        {
            this.whatPage = $id;
           
            super($type, $bubbles, $cancelable);
           
            /*trace("Type sent is: "+ $type);
           
            trace("Page selected is: "+ $id);
           
            trace("Bubbles is set to: "+$bubbles);*/
        }
        public override  function clone():Event
        {
            return new MenuEvent(type, this.whatPage, bubbles, cancelable);
        }
        public override  function toString():String
        {
            return formatToString("MenuEvent","whatPage","type","bubbles","cancelable");
        }
    }
}

Hopefully somebody could me out with this issue I'm haveing thanks!.

abe

This topic has been closed for replies.

1 reply

April 29, 2010

Yes, I got It  the issue was I had to assign my event listener to my instance of my subclass like so below!.

var _menu:GenerateMenu = new GenerateMenu(_menuText,_container);
_menu.initMenu = true;
_menu.addEventListener(MenuEvent.LOAD_PAGE, loadPage, false, 0, true);

private function loadPage(e:MenuEvent):void

{
            trace("This page loading is: "+e.whatPage);
            var _selectedPage:String = String(e.whatPage);
            _pageText.text = _selectedPage;
            _pageText.autoSize = TextFieldAutoSize.CENTER;
            _pageText.x = int(page_mc.width - _pageText.width)/2;
            _pageText.y = int(page_mc.height - _pageText.height)/2;
            _pageText.setTextFormat(_textFormat);
}