views:

716

answers:

2

Hi all i want to know what is dispatchEvent in AS3. I didn't get any idea while Googling it. :( So please help me

Edit 1:

public static  const SET_VOLUME:String = "setVolume";

private function onclick(evt:MouseEvent):void {
      soundClip.scaleX = 0;
      dispatchEvent(new Event(SET_VOLUME));

     }

what does this means?? (

+3  A: 

Basically it's a mechanism to notify objects that something has happened. When you dispatch an event, something needs to be listening for that event.

It's basically like making a telephone call ( dispatchEvent(new CallEvent(CallEvent.CALL)) ). If the person you are trying to ring isn't in, they aren't listening to the telephone event, so they can't do anything about it. If they are in ( addEventListener(CallEvent.Call, answerHandler) ), they can answer it, maybe send it straight to voice mail.

For a full rundown, try reading this article from adobe.

http://www.adobe.com/devnet/actionscript/articles/event%5Fhandling%5Fas3%5F02.html

A lot of programming languages make use of events so it's very worth while getting to grips with it.

James

Response to Edit1

It looks to me like you have a method that has been set up to handle a MouseEvent.Click on some object somewhere. When that happens it sets another objects scaleX property to 0 (making it invisible). A new Event is then Dispatched with the type set to "setVolume". This on it's own wouldn't do anything. Somewhere else this is probably a line looks a little like addEventListener("setVolume", doSomethingMethod); . When the original method is fired (seemingly a mouse click), the doSomethingMethod should fire.

James Hay
ok i have one doubt please check my questions edit 1: section
coderex
When onclick() is called the "setVolume" will be raised, notifying any subscribers (that subscribed to "setVolume" by calling addEventListener).
Richard Szalay
+2  A: 

An example might help? If you have Flash IDE, try this in your timeline:

var ball:Shape = new Shape();
ball.graphics.beginFill(0xFF0000);
ball.graphics.drawCircle(0, 0, 30);
ball.graphics.endFill();
addChild(ball);

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
addEventListener("myCustomEvent", myCustomEventListener);

function mouseMoveListener(event:MouseEvent):void
{
    dispatchEvent(new Event("myCustomEvent"));
}

function myCustomEventListener(event:Event):void
{
    ball.x = stage.mouseX;
    ball.y = stage.mouseY;
}

This is code demonstrates how the addEventListener and dispatchEvent are counterparts. The MOUSE_MOVE event is dispatched internally, but you can dispatch your own events just like the MOUSE_MOVE using dispatchEvent.

What happens in this code is that a MOUSE_MOVE from the stage is detected, but rather than handle that in the mouseMoveListener, you dispatch another event (called myCustomEvemt) which is handled in the myCustomEventListener instead. It works just like the MOUSE_MOVE event, only you dispatched the event instead of the flash player.

Hope this helps.

alecmce