Tutorial :AS3 - Dispatch Event Issue


I have an Flash AS3 project that loads external SWFs and controls them in different ways. On some of the loaded SWF files, they have a "Next Selection" button that takes you to a new presentation. On my main externally loaded SWF, I have the code:

setTimeout(function() {dispatchEvent(new Event("nextPresentation", false));}, 4000);  

Which automatically move to the next selection in the set. This code works exactly the way I want.

In the next loaded SWF, instead of having a timeout, the user goes through the whole thing where at the end of all the timelines there is a button that says next selection. So I added the following code there:

function nextSelectionClick(evt:MouseEvent) {      trace('here123');      dispatchEvent(new Event("nextPresentation", false));  }   

For some reason, that event never makes it up to the file that loaded the SWF. I'm sure I'm getting to the click event because I get the trace, but the event never makes it up even though it seems like it should be the exact same behavior as the timeout. What am I missing here? Why would that code behave different from a button click than from a timeout?



You're right in that they should be functioning the same, which leaves the giant question of what else is going on in the application. It seems as though though problem is not here, but else where.

I hate to ask a question like this, but are you sure the event is being fired from the root display object? If it is not firing from there you will not hear it externally.

Sorry, but I can't give you much more without seeing more actual code, or knowing more about the situation(s).



I think you've set the bubbles attribute to false. This will prevent the event from bubbling up any further than the container it resides in.

Try this instead:

dispatchEvent(new Event("nextPresentation", true));  

Also, regarding organization, I'd make a PresentationEvent class and have SKIP_PREVIOUS and SKIP_NEXT event constants on them. This is more consistent with the event model that ActionScript uses and it cuts down on redundancy. This way you can pass the current presentation and other handy presentation-related data as part of the event.


I am also using dispatch custom event, but sometimes its working and sometimes it isn't. For example:

private function rollBackAction  {    var atd:Class =  getDefinitionByName("com.utils."+CreateList.currentClass) as Class;    dispatchEvent(new AppEvent("RollBackAction",{}));    }  

and the using function

package com.utils  {    import com.components.mouseOverAct;  import com.model.CreateList;  import com.SequenceCheck.SeqCheck;  import com.events.AppEvent;      import flash.display.MovieClip;    import flash.events.MouseEvent;    import flash.events.Event;      public class RightRotate extends mouseOverAct  {      private var rotateBool:Boolean;      private var XposIntial:Number;      private var A:Number;      private var An:Number;      private var angle:Number;      var mc:MovieClip;      private var frameCount:int;        public function RightRotate()      {          trace("main")          this.buttonMode = true;          this.addEventListener("RollBackAction",rollingBack);          knob_mc.addEventListener(MouseEvent.MOUSE_DOWN,turn);      }      private function rollingBack(e:AppEvent)      {          trace("Abc")          knob_mc.rotation = An;          rotation_01.gotoAndStop(A);      }      private function turn(e:MouseEvent):void      {          mc = simulator.activityAssets.spw.neddle;          CreateList.excerciseName = "Right_Bottom_Right";          CreateList.currentClass = "RightRotate";          A = rotation_01.currentFrame;          An = knob_mc.rotation;          this.stage.addEventListener(MouseEvent.MOUSE_MOVE,turn);          this.stage.addEventListener(MouseEvent.MOUSE_UP,endTurn);          var position:Number = Math.atan2((mouseY - knob_mc.y),mouseX - knob_mc.x);          //trace(position);          angle = (position / Math.PI) * 180;          knob_mc.rotation = angle;          if (CreateList.stepId >= 3)          {              if (mc.currentFrame > 3)              {                  mc.gotoAndStop(mc.currentFrame + 5);                }          }          mainProcess();      }        private function endTurn(e:MouseEvent):void      {          rotateBool = false;          rotation_01.gotoAndStop(rotation_01.currentFrame);          knob_mc.removeEventListener(MouseEvent.MOUSE_DOWN,turn);          this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,turn);          knob_mc.addEventListener(MouseEvent.MOUSE_DOWN,turn);          var sqChck:SeqCheck = new SeqCheck(CreateList.stepId);        }        private function mainProcess()      {          if (! rotateBool)          {              rotateBool = true;              rotation_01.gotoAndPlay(rotation_01.totalFrames / 2 + rotation_01.currentFrame);          }      }      }    }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »