A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Dispatch Event not working!

  1. #1
    Senior Member vinayak.kadam's Avatar
    Join Date
    Oct 2006
    Location
    gotoAndPlay("Pune");
    Posts
    831

    Dispatch Event not working!

    Hey Guys!

    I am bit frustrated with this dispatch event issue....I have attached my source files....can somebody please take a look ad explain me what is wrong with me ? I am eager to know which of my basics went wrong....

    I have this class 'ClickLearnLogic.as' which listens for the custom event generated by 'ClickLearnTabs.as'.

    Thanks in advance for your kind consideration!
    Attached Files Attached Files
    As ever,
    Vinayak Kadam

  2. #2
    Senior Member
    Join Date
    Aug 2006
    Posts
    322
    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
    import flash.text.TextField;
        
    import ClickLearnModel.*;
        public class 
    Click_Learn extends MovieClip {

            public static const 
    TotalTabs:Number 3;
            public static const 
    isLinear:Boolean false;
            public var 
    tabs:Array=new Array();

            public function 
    Click_Learn():void {
                for(var 
    i=0;i<this.numChildren;i++){
                    
    tabs.push(this.getChildAt(i));
                }
                var 
    controller:ClickLearnLogic = new ClickLearnLogic(tabs,this);
            }

        }

    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.MouseEvent;
        
    import flash.text.TextField;
        
    import ClickLearnModel.*;
        public class 
    ClickLearnLogic extends MovieClip {

            public var 
    target:MovieClip;

            public function 
    ClickLearnLogic(tabsArr,target):void {
                
    this.target=target;
                
    trace("Waiting here for the custom event");
                for (var 
    i=0i<tabsArr.lengthi++) {
                    
    tabsArr[i].addEventListener(ClickLearnEvent.ON_TAB_CLICK,fnShowPopup);
                }
            }
            private function 
    fnShowPopup(evt:ClickLearnEvent):void {
                
    trace("event received");
                
    target.txt.text=evt.target.name;
            }
        }

    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
    import flash.events.MouseEvent;
        public class 
    ClickLearnTabs extends MovieClip {

            private var 
    tabClickedEvent:ClickLearnEvent;

            public function 
    ClickLearnTabs():void {
                
    this.gotoAndStop("active");
                
    addEventListener(MouseEvent.MOUSE_UP,fnTabClicked);
            }
            private function 
    fnTabClicked(Evt:MouseEvent):void {
                
    tabClickedEvent = new ClickLearnEvent(ClickLearnEvent.ON_TAB_CLICK);
                
    trace("Event is created but dispatch code below does not fires");
                
    dispatchEvent(tabClickedEvent);
            }
        }

    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        public class 
    ClickLearnEvent extends Event {
            
            public static  const 
    ON_TAB_CLICK:String "onTabClick";
            
            public function 
    ClickLearnEvent(type:Stringbubbles:Boolean=falsecancelable:Boolean=false):void {
                
    super(typebubblescancelable);
            }
        }

    Hope this will work for you..

  3. #3
    Senior Member vinayak.kadam's Avatar
    Join Date
    Oct 2006
    Location
    gotoAndPlay("Pune");
    Posts
    831

    Thumbs up Thank you Sir!

    Hey good to see you again.....

    Your code resolved my issue but can I please request you to explain at a conceptual level what I was doing wrong and how u resolved it ?
    As ever,
    Vinayak Kadam

  4. #4
    Senior Member
    Join Date
    Aug 2006
    Posts
    322
    Though I gave you the previous solution, this is more right approach to do what you are trying:

    Document Class

    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
    import flash.events.MouseEvent;
        
    import flash.text.TextField;
        
    import classes.*;

        public class 
    DocClass extends MovieClip {
            public static  const 
    TotalTabs:Number 3;
            public static  const 
    isLinear:Boolean false;
            public var 
    controller:ClickTabLogic;
            private var 
    tabsList:Array;

            public function 
    DocClass():void {
                
    tabsList=new Array(tab1,tab2,tab3);
                
    controller = new ClickTabLogic(this);
                
                for (var 
    i=0i<tabsList.lengthi++) {
                    
    tabsList[i].addEventListener(MouseEvent.CLICK,tabClicked);
                }
            }
            private function 
    tabClicked(evt:MouseEvent):void {
                
    controller.setTab(evt);
            }
        }

    Base Class which attached with the Button Symbols

    PHP Code:
    package classes{
        
    import flash.display.MovieClip;

        public class 
    ActiveButton extends MovieClip {

            public function 
    ActiveButton() {
                
    this.gotoAndStop("active");            
            }
        }

    This Class is listening whenever events are dispatches

    PHP Code:
    package classes{
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
    import flash.text.TextField;

        public class 
    ClickTabLogic extends MovieClip {
            public var 
    tabs:ClickTabs;
            private var 
    target;
            public function 
    ClickTabLogic(target):void {
                
    this.target=target;
                
    trace("Waiting here for the custom event");
                
    tabs=new ClickTabs();
                
    tabs.addEventListener(ClickTabEvent.ON_TAB_CLICK,fnShowPopup);
            }
            private function 
    fnShowPopup(evt:ClickTabEvent):void {
                
    target.txt.text=(evt.target.tabName);
            }
            public function 
    setTab(evt) {
                
    tabs.clickedListener=(evt);
            }
        }

    This Class is despatching event

    PHP Code:
    package classes{
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        public class 
    ClickTabs extends MovieClip {
            private var 
    tabClickedEvent:ClickTabEvent;
            public var 
    tabName;
            public function 
    ClickTabs() {
            }
            public function 
    set clickedListener(evt):void {
                
    tabName=evt.target.parent.name;
                
    tabClickedEvent = new ClickTabEvent(ClickTabEvent.ON_TAB_CLICK);
                
    trace("Event is created but dispatch code below does not fires");
                
    dispatchEvent(tabClickedEvent);
            }
        }

    This is the constant Class which extends Event Class

    PHP Code:
    package classes{
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
        public class 
    ClickTabEvent extends Event {
            
            public static  const 
    ON_TAB_CLICK:String "onTabClick";
            
            public function 
    ClickTabEvent(type:Stringbubbles:Boolean=falsecancelable:Boolean=false):void {
                
    super(typebubblescancelable);
            }
        }

    There are many approaches on dispatching and listening custom events, but out of all there are two type of methods of doing those. Those are push and pull methods. The previous solution I shown you was a dirty way. This is a push method and is more flexible for oop programming.

    Try to go through the code, If you still fail to understand how it works, just let me know, I'll try to explain in detail of both the solutions and there dependencies in programming structure.


    Regards
    Last edited by marlopax; 03-08-2013 at 06:10 PM.

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