Countdown in actionscript 3 - actionscript-3

How do I create a countdown for a bus schedule in actionscript 3, I've searched online but mostly countdowns for holidays or a specific date appear. I need it to be recurring, bus runs every 30 mins.

package
{
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class countdownTimer extends Sprite
{
private var timer:Timer = new Timer(1000);
private var countdown:Number = 60;
public function countdownTimer():void
{
init();
}
private function init():void
{
timer.start();
timer.addEventListener(TimerEvent.TIMER, action);
}
private function action(e:TimerEvent):void
{
countdown--;
trace(countdown);
}
}
}
Its just basic..hope it will helps u.

I made a small flash application to give you an idea as to how you can approach a bus schedule with a countdown:
schedule.xml:
<?xml version="1.0" encoding="utf-8" ?>
<schedule>
<bus>
<name>1</name>
<times>
<time>1300</time>
<time>1400</time>
<time>1500</time>
<time>1600</time>
<time>1700</time>
</times>
</bus>
<bus>
<name>2</name>
<times>
<time>1300</time>
<time>1400</time>
<time>1500</time>
<time>1600</time>
<time>1700</time>
</times>
</bus>
</schedule>
Main.as(document class):
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.Timer;
[SWF(width="200", height="350", backgroundColor="0xFFFFFF", frameRate="32")]
public class Main extends Sprite
{
private var _scheduleUi:ScheduleUI;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var urlLoader:URLLoader = new URLLoader(new URLRequest("xml/schedule.xml"));
urlLoader.addEventListener(Event.COMPLETE, onUrlLoaderComplete);
}// end function
private function onUrlLoaderComplete(e:Event):void
{
_scheduleUi = new ScheduleUI(XML(URLLoader(e.target).data));
addChild(_scheduleUi);
}// end function
}// end class
}// end package
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
internal class ScheduleUI extends Sprite
{
private var _schedule:Schedule;
private var _scheduleTextField:TextField;
public function ScheduleUI(scheduleXml:XML)
{
_schedule = new Schedule(scheduleXml);
_scheduleTextField = new TextField();
_scheduleTextField.autoSize = TextFieldAutoSize.LEFT;
_scheduleTextField.multiline = true;
addChild(_scheduleTextField);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}// end function
private function onEnterFrame(e:Event):void { updateScheduleTextField() }
private function updateScheduleTextField():void
{
var scheduleString:String = new String();
for each(var bus:Bus in _schedule.getBuses())
{
scheduleString += "BUS: " + bus.name;
for each(var time:Time in bus.getTimes())
{
scheduleString += "\n\n" + "\t" + "Time: " + time + ", Counddown: " + time.getCountDown();
}// end for each
scheduleString += "\n\n";
}// end for each
_scheduleTextField.text = scheduleString;
}// end function
}// end class
internal class Schedule
{
private var _schedule:XML;
public function Schedule(schedule:XML):void
{
_schedule = schedule;
}// end function
public function getBuses():Vector.<Bus>
{
var buses:Vector.<Bus> = new Vector.<Bus>();
for each(var bus:XML in _schedule.bus) buses.push(new Bus(bus));
return buses;
}// end function
}// end function
internal class Bus
{
private var _bus:XML;
public function get name():int { return _bus.name }
public function Bus(bus:XML) { _bus = bus }
public function getTimes():Vector.<Time>
{
var times:Vector.<Time> = new Vector.<Time>();
for each(var time:XML in _bus.times.time) times.push(new Time(time));
return times;
}// end function
}// end class
internal class Time
{
private var _time:XML;
public function Time(time:XML) { _time = time }
public function getCountDown():String
{
var hours:int = _time.toString().substring(0, 2);
var minutes:int = _time.toString().substring(2, 4);
var currentDate:Date = new Date();
var date:Date = new Date();
date.setUTCMilliseconds(0);
date.setUTCSeconds(0);
date.setUTCMinutes(minutes);
date.setUTCHours(hours - 1);
var countDownDate:Date = new Date();
countDownDate.setTime((date.getTime() - currentDate.getTime()))
return (countDownDate.getTime() > 0) ? countDownDate.toTimeString().substring(0, 8) : String(0);
}// end function
public function toString():String { return _time }
}// end class

Related

AS3 - Get button instance name and path

How to trace that "section1.btnback" was clicked?
section1.btnback.addEventListener(MouseEvent.CLICK, getBack)
function getBack(event:MouseEvent):void
{
// trace: "section1.btnback"
}
Thanks.
Uli
I'm not sure what actualy you are asking for. If my suggestion is not the right for you, please excuse me. If you write
trace(event.target)
you will see the complete name of the instance of the button.
I am guessing that you want it to say something like:
"Button was clicked!" to console each time the button is pressed?
trace("Section1.btnback was clicked!");
I believe.
(Wasn't really sure what you were asking for)
-Expanded in comments below-
If you want the "complete path" like meddlingwithfire suggested in his comment on 3lionz's answer, then you can use a class I created that does it for you. The class is called DOPath(short for display object path). The following is an example of it being used to get the complete path of a display object:
Main.as(document class):
package
{
import com.example.display.DOPath;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var container1:Sprite = new Sprite();
container1.name = "container1";
var container2:Sprite = new Sprite();
container2.name = "container2";
var square:Square = new Square();
square.name = "square";
square.addEventListener(MouseEvent.CLICK, onSquareClick);
addChild(container1);
container1.addChild(container2);
container2.addChild(square);
}// end function
private function onSquareClick(e:MouseEvent):void
{
var square:Square = e.target as Square;
var doPath:DOPath = new DOPath(square);
trace(doPath); // output: stage.root1.container1.container2.square
}// end function
}// end package
}// end package
import flash.display.Sprite;
internal class Square extends Sprite
{
public function Square()
{
graphics.beginFill(0xFF0000);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
}// end function
}// end class
DOPath.as:
package com.example.display
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Stage;
public class DOPath
{
private var _parents:Vector.<DisplayObjectContainer>;
private var _d:DisplayObject;
public function DOPath(d:DisplayObject):void
{
_d = d;
init();
}// end function
private function init():void
{
_parents = new Vector.<DisplayObjectContainer>;
pushParent(_d);
}// end function
private function pushParent(d:DisplayObject):void
{
if(d.parent)
{
_parents.push(d.parent);
pushParent(d.parent);
}// end if
}// end function
public function toString():String
{
var path:String = _d.name;
for (var i:uint = 0; i < _parents.length; i++)
{
var name:String = (_parents[i] is Stage) ? "stage" : _parents[i].name;
path = name + "." + path;
}// end for
return path;
}// end function
}// end class
}// end package
You can use a quite simple recursive function like this one:
private function getDOname(dob : DisplayObject) : String
{
var result : String = dob.name;
if (dob is UIComponent)
{
result = UIComponent(dob).id;
}
if(dob.parent && dob.parent is DisplayObjectContainer)
{
result = getDOname(dob.parent) + "." + result;
}
return result;
}
You can pass event.target to the function and acquire the button that was clicked. If you are using these techniques for debug purposes, you can simply add and event listener for click event to the stage or the SandboxRoot itself, if you are unsure which visual element you are actually clicking.

How to manage loaded assets?

I'm working on an AS3 project that must load a lot of external files; images, sound clips, movie clips etc. The program first loads an XML file that contains the file names of all assets needed by each class. From that point on I'm not sure what would be the best way to distribute those assets.
I thought about making a dedicated assets class that will load everything the XML object describes, and then pass it to each class' constructor so they can access the objects they need. This seems like the "tidiest" option, but is there a best practice way for managing loaded assets?
You could create a class that handles loading the assets and storing them as bytes. Then you can get an asset from that class via a reference(e.g. name) and do what you need with it. I've created an example to demonstrate this:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.events.Event;
public class Main extends Sprite
{
private var _assets:Assets;
private var _counter:int;
private var _assetsXml:XML;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_assetsXml = <assets>
<asset name="chrysanthemum" url="assets/chrysanthemum.jpg"></asset>
<asset name="kalimba" url="assets/kalimba.mp3"></asset>
<asset name="wildlife" url="assets/wildlife.wmv"></asset>
</assets>;
_assets = new Assets();
for each(var asset:XML in _assetsXml.children())
{
_assets.addEventListener(Event.COMPLETE, onAssetComplete);
_assets.load(asset.#name, asset.#url);
}// end for each
}// end function
private function onAssetComplete(e:Event):void
{
if (++_counter == _assetsXml.children().length()) addImage();
}// end function
private function addImage():void
{
var loader:Loader = new Loader()
loader.loadBytes(_assets.getAssetBytesByName("chrysanthemum"));
addChild(loader);
}// end function
}// end class
}// end package
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
internal class Assets extends EventDispatcher
{
private var _assetLoaders:Vector.<AssetLoader>;
public function Assets():void
{
_assetLoaders = new Vector.<AssetLoader>();
}// end class
public function load(name:String, url:String):void
{
var assetLoader:AssetLoader = new AssetLoader(name);
assetLoader.addEventListener(Event.COMPLETE, onAssetLoaderComplete);
assetLoader.load(url);
}// end function
public function getAssetBytesByName(name:String):ByteArray
{
for each(var assetLoader:AssetLoader in _assetLoaders)
{
if (assetLoader.name == name) break;
}// end for each
return assetLoader.bytes;
}// end function
private function onAssetLoaderComplete(e:Event):void
{
var assetLoader:AssetLoader = e.target as AssetLoader;
_assetLoaders.push(assetLoader);
dispatchEvent(new Event(Event.COMPLETE));
}// end function
}// end class
internal class AssetLoader extends EventDispatcher
{
private var _name:String;
private var _url:String;
private var _bytes:ByteArray;
public function get name():String { return _name }
public function get url():String { return _url };
public function get bytes():ByteArray { return _bytes };
public function AssetLoader(name:String):void
{
_name = name;
}// end function
public function load(url:String):void
{
_url = url;
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, onUrlLoaderComplete);
urlLoader.load(new URLRequest(_url));
}// end function
private function onUrlLoaderComplete(e:Event):void
{
var urlLoader:URLLoader = e.target as URLLoader;
_bytes = ByteArray(urlLoader.data);
dispatchEvent(new Event(Event.COMPLETE));
}// end function
}// end class

how to manage event listeners in for loop?

For loop contains an event listener function. but loop iteration is quicker than listener function.Before the listener function completes next iteration starts. how to handle this???
Sounds like you don't need a "for" loop.
According to your description, it looks like you need some form of event chain where after the first event complete you set up a new listener and so on...
If the answer your looking for is as PatrickS described, I've create an example of how to implement it:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite
{
private var _queue:Queue;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var queueItems:Vector.<QueueItem> = new Vector.<QueueItem>();
for (var i:uint = 0; i < 5; i++)
{
var timer:Timer = new Timer(2 * 1000, 1);
queueItems.push(new QueueItem(timer, TimerEvent.TIMER_COMPLETE, onTimerComplete));
}// end for
_queue = new Queue(queueItems);
var currentTimer:Timer = Timer(_queue.currentQueueItem.eventDispatcher);
currentTimer.start();
_queue.shift();
}// end function
private function onTimerComplete(e:TimerEvent):void
{
if (_queue.currentQueueItem)
{
var currentTimer:Timer = Timer(_queue.currentQueueItem.eventDispatcher);
currentTimer.start();
if (_queue.length > 0) _queue.shift();
}// end if
}// end function
}// end class
}// end package
import flash.events.IEventDispatcher;
import flash.events.Event;
internal class Queue
{
private var _queueItems:Vector.<QueueItem>;
public function get currentQueueItem():QueueItem { return (_queueItems.length > 0) ? _queueItems[0] : null }
public function get length():int { return _queueItems.length }
public function Queue(queueItems:Vector.<QueueItem>)
{
_queueItems = queueItems;
}// end function
public function shift():void
{
var eventDispatcher:IEventDispatcher = _queueItems[0].eventDispatcher;
var type:String = _queueItems[0].type;
var listener:Function = _queueItems[0].listener;
var useCapture:Boolean = _queueItems[0].useCapture;
var priority:int = _queueItems[0].priority;
var useWeakReference:Boolean = _queueItems[0].useWeakReference;
eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
_queueItems.shift();
}// end function
}// end class
internal class QueueItem
{
private var _eventDispatcher:IEventDispatcher;
private var _type:String;
private var _listener:Function;
private var _useCapture:Boolean;
private var _priority:int;
private var _useWeakReference:Boolean;
public function get eventDispatcher():IEventDispatcher { return _eventDispatcher }
public function get type():String { return _type}
public function get listener():Function { return _listener }
public function get useCapture():Boolean { return _useCapture }
public function get priority():int { return _priority }
public function get useWeakReference():Boolean { return _useWeakReference }
public function QueueItem(eventDispatcher:IEventDispatcher, type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false)
{
_eventDispatcher = eventDispatcher;
_type = type;
_listener = listener;
_useCapture = useCapture;
_priority = priority;
_useWeakReference = useWeakReference;
}// end function
}// end class
Simply put, this creates a queue like system that adds an event listener to the IEventListener object at the front of the queue and when its added the queue is shifted.
The above shows how you can use this with Timer objects.

About class(es) in ActionScript

I have one folder(Projects), which contains 2 files: FirstPro.fla, FirstClass.as
The goal of my question is interaction with two different objects (MovieClip) from class, which i created by myself, i have only one class, and it class for only one MovieClip, my goal is interaction, for MovieClip second which has name(letterPanel) has no class, its only MovieClip on scene;
In FirstClass.as i have the next code:
package
{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
public class FirstClass extends MovieClip
{
public function FirstClass()
{
var NewMyTimer:Timer = new Timer(100);
NewMyTimer.addEventListener(TimerEvent.TIMER, hm);
NewMyTimer.start();
}
public function hm(TimerEvent):void
{
this.y += 5;
if(this.hitTestObject(letterPanel))
{
trace("Coincidens");
}
}
}
}
And in FirstPro.fla file i have on the scene i have two MovieClip (Images), one of them has firstobj name, which has link with my created class, and it works, my object firstobj going down with the timer, but i would like to interaction with the second object(MovieClip-letterPanel) - if(this.hitTestObject(letterPanel)) { trace("Coincidens"); }
If i write this code in my class this output mistake (letterPanel) is not undefined, what can i do with interactions of those two object in one class,?
You have to assign the MovieClip and you also can not start the timer untill that var has been set
package{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
public class FirstClass extends MovieClip{
private var _letterPanel:MovieClip;
public function FirstClass(){
}
public function startConter( ):void{
var NewMyTimer:Timer=new Timer(100);
NewMyTimer.addEventListener(TimerEvent.TIMER, hm);
NewMyTimer.start();
}
public function hm(TimerEvent):void{
this.y+=5;
if(this.hitTestObject(this._letterPanel)){
trace("Coincidens");
}
}
public function set letterPanel( val:MovieClip ):void{
this._letterPanel = val;
}
}
}
var firstClass:FirstClass = new FirstClass( );
firstClass.letterPanel = letterPanel;
firstClass.startCounter( );
My initial response(other than being confused, since your explaination of your project is a bit hard to understand) is that you don't have a good grasp on OOP. There was an answer for this question before that I believed to be correct. If I remember correctly you rejected it simply because it didn't utilize classes. OOP is about knowing how to as well as when to implement OOP concepts such as abstraction, encapsulation, inheritance, polymorphism etc.
In your case I think the simple answer to your question is the following:
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event):void
{
if(displayObject1.hitTestObject(displayObject2)
trace("hit");
}// end function
However if you feel the need to truely use classes, I've created a similar flash application to demonstrate how you would go about it. Its basically 3 draggable red, green and blue circle display objects on the stage and when you drag a circle display object and it overlaps/intersects another circle display object a trace is made to describe the collision. You can run the application by simply copying and pasting the following code into your document class:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var redCircle:Circle = new Circle(Color.RED, 50);
redCircle.name = "redCircle";
redCircle.x = 100;
redCircle.y = 100;
addChild(redCircle);
var greenCircle:Circle = new Circle(Color.GREEN, 50);
greenCircle.name = "greenCircle";
greenCircle.x = 300;
greenCircle.y = 100;
addChild(greenCircle);
var blueCircle:Circle = new Circle(Color.BLUE, 50);
blueCircle.name = "blueCircle";
blueCircle.x = 500;
blueCircle.y = 100;
addChild(blueCircle);
var collision:Collision = new Collision(stage, redCircle, greenCircle, blueCircle);
collision.addEventListener(CollisionEvent.COLLISION, onCollision);
}// end function
private function onCollision(e:CollisionEvent):void
{
var collision:Collision = Collision(e.target);
collision.removeEventListener(CollisionEvent.COLLISION, onCollision);
trace("There was a collision between " + e.displayObject1.name + " and " + e.displayObject2.name);
}// end function
}// end class
}// end package
internal class Color
{
public static const RED:uint = 0xFF0000;
public static const GREEN:uint = 0x00FF00;
public static const BLUE:uint = 0x0000FF;
}// end class
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
internal class Circle extends Sprite
{
public function Circle(color:uint, radius:Number)
{
graphics.beginFill(color);
graphics.drawCircle(0,0,radius);
graphics.endFill();
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}// end function
private function onMouseDown(e:MouseEvent):void
{
parent.setChildIndex(this, parent.numChildren - 1);
startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)
}// end function
private function onMouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stopDrag();
}// end function
}// end class
internal class Collision extends EventDispatcher
{
private var _stage:Stage;
private var _doVector:Vector.<DisplayObject>;
public function Collision(stage:Stage, ...displayObjects)
{
_stage = stage;
_doVector = getDOVector(validateArgs(displayObjects, DisplayObject));
init();
}// end function
private function init():void
{
_stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}// end function
private function onEnterFrame(e:Event):void
{
for(var i:uint = 0; i < _doVector.length; i++)
{
for(var j:uint = 0; j < _doVector.length; j++)
{
if (_doVector[i] != _doVector[j])
{
if (_doVector[i].hitTestObject(_doVector[j]))
dispatchEvent(new CollisionEvent(CollisionEvent.COLLISION, _doVector[i], _doVector[j]));
}// end if
} // end for
}// end for
}// end function
private function validateArgs(args:Array, type:Class):Array
{
for each(var arg:* in args)
if(!(arg is type))
throw new ArgumentError("Argument must be of type " + type);
return args;
}// end function
private function getDOVector(array:Array):Vector.<DisplayObject>
{
var doVector:Vector.<DisplayObject> = new Vector.<DisplayObject>();
for each(var displayObject:DisplayObject in array)
doVector.push(displayObject);
return doVector;
}// end function
}// end class
internal class CollisionEvent extends Event
{
public static const COLLISION:String = "collision";
private var _displayObject1:DisplayObject;
private var _displayObject2:DisplayObject;
public function get displayObject1():DisplayObject { return _displayObject1 };
public function get displayObject2():DisplayObject { return _displayObject2 };
public function CollisionEvent(type:String,
displayObject1:DisplayObject,
displayObject2:DisplayObject,
bubbles:Boolean = false,
cancelable:Boolean = false):void
{
_displayObject1 = displayObject1;
_displayObject2 = displayObject2;
super(type, bubbles, cancelable);
}// end function
override public function clone():Event
{
return new CollisionEvent(type, displayObject1, displayObject2, bubbles, cancelable);
}// end function
}// end class

Remove all ENTER_FRAME listeners

Is there a way to remove ALL ENTER_FRAME event listeners at once?
Only if you've been keeping careful track of your listeners. There is no "removeAll" type functionality for event listeners, so it helps to collect your listeners in an easy-to-manage place.
An option is to create a class that handles adding event listeners to IEventDispatcher objects, registering those objects and removing all events listeners at the same time. The following is a example I quickly made to demonstrate this:
EventListeners.as:
package
{
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.utils.Dictionary;
public class EventListeners
{
private var _objectDictionaries:Vector.<Dictionary>;
public function EventListeners()
{
init();
}// end function
public function addEventListener(object:IEventDispatcher,
type:String,
listener:Function,
useCapture:Boolean = false,
priority:int = 0,
useWeakReference:Boolean = false)
{
object.addEventListener(type, listener, useCapture, priority, useWeakReference);
var objectDictionary:Dictionary = new Dictionary();
objectDictionary["object"] = object;
objectDictionary["type"] = type;
objectDictionary["listener"] = listener;
objectDictionary["useCapture"] = useCapture;
_objectDictionaries.push(objectDictionary);
}// end function
public function removeAll():void
{
for each(var objectDictionary:Dictionary in _objectDictionaries)
{
var object:IEventDispatcher = objectDictionary["object"] as IEventDispatcher;
var type:String = objectDictionary["type"] as String;
var listener:Function = objectDictionary["listener"] as Function;
var useCapture:Boolean = objectDictionary["useCapture"] as Boolean;
object.removeEventListener(type, listener, useCapture);
}// end for
init();
}// end function
private function init():void
{
_objectDictionaries = new Vector.<Dictionary>();
}// end function
}// end class
}// end package
Main.as:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
private var _eventListeners:EventListeners;
private var _sprite1:Sprite;
private var _sprite2:Sprite;
public function Main()
{
init();
}// end function
public function init():void
{
_eventListeners = new EventListeners();
_sprite1 = new Sprite();
_sprite2 = new Sprite();
_eventListeners.addEventListener(_sprite1, Event.ENTER_FRAME, enterFrameHandler);
_eventListeners.addEventListener(_sprite2, Event.ADDED_TO_STAGE, addedToStageHandler);
trace("_sprite1.hasEventListener = " + _sprite1.hasEventListener(Event.ENTER_FRAME));
trace("_sprite2.hasEventListener = " + _sprite2.hasEventListener(Event.ADDED_TO_STAGE));
_eventListeners.removeAll();
trace("\n_eventListeners.removeAll() invoked\n");
trace("_sprite1.hasEventListener = " + _sprite1.hasEventListener(Event.ENTER_FRAME));
trace("_sprite2.hasEventListener = " + _sprite2.hasEventListener(Event.ADDED_TO_STAGE));
}// end function
private function enterFrameHandler(e:Event):void {};
private function addedToStageHandler(e:Event):void {};
}// end class
}// end package
The output from running this example is the following:
_sprite1.hasEventListener = true
_sprite2.hasEventListener = true
_eventListeners.removeAll() invoked
_sprite1.hasEventListener = false
_sprite2.hasEventListener = false