why are my dispatchEvent events dispatching more than once?

By brianemsu | Jun 17, 2009

actionscript3

Hi,

I have a question about some strange behavior I’m seeing from a couple of my dispatchEvent commands. There is one for each of the two methods in my Background class, they basically just tell Main (my controller) when an image has been passed into Background.
Here’s background:

ActionScript Code:
package com.bee.display {     import flash.display.Sprite;     import flash.events.Event;         import gs.TweenLite;     import gs.easing.Cubic;         public class Background extends Sprite     {         public static const IMAGE_ADDED:String = “added”;         public static const COMPLETE:String = “complete”;         public var _currentChild:Sprite;                         public function Background()         {         }                 public function add(image:Sprite):void         {             if(!_currentChild)             {                 _currentChild = image;                 _currentChild.alpha = 0;                 addChild(_currentChild);                 trace(“inside”);                 dispatchEvent(new Event(Background.IMAGE_ADDED));                 TweenLite.to(_currentChild, .5, {alpha:1, ease:Cubic.easeInOut});             }         }         public function crossfade(next:Sprite):void         {             next.alpha = 0;             addChild(next);             if(_currentChild)             {                 TweenLite.to(_currentChild, 1.5, {alpha:0, ease:Cubic.easeInOut});                 TweenLite.to(next, 1.5, {alpha:1, ease:Cubic.easeInOut, onComplete:completeEvent});                 function completeEvent():void                 {                     removeChild(_currentChild);                     _currentChild = next;                     trace(“dispatch COMPLETE”);                     dispatchEvent(new Event(Background.COMPLETE));                 }             }             else             {                 throw new Error(“can’t crossfade when empty”);             }         }     } }

an example of the code in main is:

ActionScript Code:
_background.addEventListener(Background.IMAGE_ADDED, onAdded); private function onAdded(e:Event):void         {             trace(“added”);         }

in the output panel, I’m getting multiple "added" before the method is even called… any ideas on what could be causing this?

-brian

actionscript3

Please reply at our Forum

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments

© 2007 ActionScript 3.0