A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: addEventListener of a linked class doesn't work!!!

  1. #1
    Member
    Join Date
    Oct 2007
    Location
    Vancouver,BC
    Posts
    38

    Question addEventListener of a linked class doesn't work!!!

    Hey guys,
    I have a small problem with linked custom classes [that do extend MovieClip] not receiving events sent by the document class. Here is the code:
    Document class:
    Code:
    package {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	
             public class Main extends MovieClip {
    	 
    	       public function Main() {
    	 	 dispatchEvent(new Event("blah"));
    		 trace("Main ");
    	       }
    	 
    	}
    }
    Foo class:

    Code:
    package {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	public class Foo extends MovieClip {
    		function Foo() {
    			trace("Foo");
    			addEventListener("blah", workDarnIt);
    		}
    		
    		function workDarnIt(evt:Event):void{
    			trace("got it in Foo");
    		}
    	}
    }
    Bar class:

    Code:
    package {
    	import Foo;
    	import flash.events.Event;
    	
    	public class Bar extends Foo {
    		
    		function Bar() {
    			super();
    			trace("Bar");
    			//addEventListener("blah", workDarnIt);
    		}
    		
    		override function workDarnIt(evt:Event):void{
    			trace("got it in Bar");
    		}
    	}
    }
    The Bar class is linked to a simple square MovieClip that was placed on Stage during author time.
    When I compile the .fla the it never traces "got it in bar" ?! It also never traces "got it in Foo" ?! It's as if dispatchEvent() function in the Main document class does nothing... I know for a fact that The Main class does fire an event, 'cause I tested it. But I don't understand why the Bar class doesn't hear the event "blah" from the Main? The constructors of the linked classes run before the Main() document class constructor. I know that, so why doesn't this work?

    BTW: if i say stage.addEventListener() inside Bar class everything works properly. It's as if the linked Bar class is not part of DisplayList!!! How can this happen? (Bar extends Foo that extends MovieClip, and I can see the Bar square Movieclip on stage when movie runs.)

    Please help! Thank you in advance.
    Attached Files Attached Files

  2. #2
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Main:
    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
        public class 
    Main extends MovieClip {
             public function 
    Main() {
                
             }
        }

    Foo:
    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
        public class 
    Foo extends MovieClip {
            function 
    Foo() {
                
    this.addEventListener("blah", ****you);
            }
            
            function ****
    you(evt:Event):void {
                
    trace("got it in Foo");
            }
        }

    Bar:
    PHP Code:
    package {
        
    import Foo;
        
    import flash.events.Event;
        
        public class 
    Bar extends Foo {
            function 
    Bar() {
                
    // Dispatch event to the Foo class
                
    this.dispatchEvent(new Event("blah"));
            }
        }


  3. #3
    Member
    Join Date
    Oct 2007
    Location
    Vancouver,BC
    Posts
    38
    OK, but I want the Main class to dispatch the Event, not the Bar class. The reason is:
    The Bar constructor Bar() runs BEFORE the Main(); therefore, when the Bar dispatches the event the Main constructor cannot listen to it [because it's not initialized yet...] That's the whole problem. I need Main to send an event to its children "ARE_YOU_READY" and I need the children (in this case Bar) to reply "BAR_IS_READY"...something like that. Any other guesses ? Thanks.

  4. #4
    Member
    Join Date
    Oct 2007
    Location
    Vancouver,BC
    Posts
    38
    People please help me with this. I'm running out of time here. I need to fix [understand] this today. How can I get around this problem? I need to do this because I have tens of child movieclips [without instance names] on the stage and I need to keep track of them somehow, so I need the main class to send the event when Main constructor runs and I need the children to respond back to Main class with the reference to them self in the event object. I then plan to put the references of all children that respond into an array, so I can work with them later... that's the main idea that what i'm trying to work out. Please help! Many thanks.

  5. #5
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Will this accomplish what your going for?

    Main:
    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.Event;

        public class 
    Main extends MovieClip {
            private var 
    myFoo:Foo;
            
            public function 
    Main() {
                
    myFoo = new Foo();
                
    myFoo.addEventListener(Event.ADDEDfooAdded);
                
                
    this.addChild(myFoo);
            }
            
            private function 
    fooAdded(e:Event):void 
            
    {
                
    // Wait until the Foo object is added, then dispatch the event
                
    myFoo.dispatchEvent(new Event("blah"));
            }
        }

    Foo:
    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.Event;
        
        public class 
    Foo extends MovieClip {
            function 
    Foo() {
                
    // Listen for the event
                
    this.addEventListener("blah"onFooBlah);
            }
            
            function 
    onFooBlah(e:Event):void{
                
    trace("Got it in Foo");
            }
        }


  6. #6
    Member
    Join Date
    Oct 2007
    Location
    Vancouver,BC
    Posts
    38
    This wight not work in my case, because I cannot instanciate Foo [or Bar] classes dynamically. [i.e. with foo = new Foo()] In fact, all instances of Foo and Bar classes are already on the stage! [they were put there by a designer during author-time] So, by the time the Main() is run all Foo()'s are already instantiated. The core of the problem here is that custom classes that have linkages to movieclips on stage do not appear to be part of global DisplayList; (how can that be I do not understand) therefore they cannot listen to events sent by the DocumentClass.... that's my theory anyway... and it is freaking me out! Any other ideas? Thanks for your help sstalder. I appreciate it.

  7. #7
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Ok, here is my last attempt... this time I think I finally understand what your doing (sorry).

  8. #8
    Member
    Join Date
    Oct 2007
    Location
    Vancouver,BC
    Posts
    38
    Nice try sstalder but I don't have a reference to myBar, because the child movie clips on stage are not named at all. That's the whole point of this painful exercise is to figure out a way to find references to movie clips that have no instance name. I know they are in memory though, because we can see them on the stage. I think internally they are called like: "instance_0000234" ... so yea... thank you for your help. I'll just have to figure something out.... Anybody else out there knows how to solve it? Thanks people.

  9. #9
    Member
    Join Date
    Oct 2007
    Location
    Vancouver,BC
    Posts
    38

    resolved It's elementary Watson!

    Ok I found a work-around to this problem.
    Apparently, the event model will not help you. It just doesn't work properly in this case. (I'm still unsure why exactly) Anyway here is what i did:

    Code:
    package {
    	import flash.display.MovieClip;
    	
    	/**
    		An example (hack) of how to get the references to unnamed movieclips 
    		that are put on stage at author time.
    		Run the .fla and drag any number of movieclips from the library to the stage
    		at author time. You don't have to name the instances. Just compile.
    		--"Look Ma, no hands!" :)
    	*/
    	public class DocClass extends MovieClip {
    	 
    	 //Remember the OO compiler initialization rules?
    	 //The variables get created first! Even before the constructors(and functions) of ANY classes!
    	 //I'm using this concept here to create a public array that's visible for all objects in a SWF
    	 //even before their constructors are run. 
    	 public var items_arr:Array = new Array();
    	 
    	 public function DocClass() {
    		/*
    			By the time the document class constructor runs all its children are already inited.
    		*/
    		for each(var b:Bar in items_arr) {
    			trace(b.getName());
    			//do whatever you want here. Now you have control over unnamed movieclips on the stage.
    		}
    	 }
     
    	}
    }
    Foo Class:

    Code:
    package {
    	import flash.display.MovieClip;
    
    	public class Foo extends MovieClip {
    		
    		private const __name:String = "I'm a Foo";
    		
    		public function Foo() {
    			//trace(__name);
    			DocClass(this.parent).items_arr.push(this); //here, "this" refers to Bar!
    		}
    	
    	}
    }
    Bar class:

    Code:
    package {
    	import Foo;
    
    	public class Bar extends Foo {
    		
    		private const __name:String = "I'm a Bar";
    		
    		public function Bar() {
    			super(); //call the constructor of super class (in this case Foo)
    			//from this point on, the reference "&" to this object is inside the
    			//items_arr inside the DocClass.
    			//trace(__name);
    		}
    		
    		public function getName():String {
    			return this.__name;
    		}
    	}
    }
    I'm attaching the .fla too... Run the fla and you'll see a way to control unnamed movieclips that are put on the stage during authortime.
    Thanks for your help
    Attached Files Attached Files

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