www.AS:Main.com
AsBroadcaster
Introduction: AsBroadcaster is something which is used to create your own events. For example, the onEnterFrame is an event that is called every time at the beginning of each frame, onMouseDown is called when the mouse is clicked, etc. With AsBroadcaster, you can create your own events for Flash.
How: AsBroadcaster is different to most other classes as it is not created like this:
var asb:AsBroadcaster = new AsBroadcaster();
Instead you must create a new object and then tell Flash to use that object as the broadcaster like so:
var asb:Object = new Object();
AsBroadcaster.initialize(asb);
Now Flash knows that you want to broadcast messages using this object as the broadcaster, simple enough right? Ok, now to send out these messages. I'm going to use a simple one as an example which will be called whenever the mouse is dragged held down, so we will do it like this:
var asb:Object = new Object();
var asb2:Object = new Object();
AsBroadcaster.initialize(asb);
asb2.onMouseDown = function() {
bla = setInterval(function () {
asb.broadcastMessage("onMouseDrag");
}, 1000/50); //Change 50 to your framerate
};
asb2.onMouseUp = function() {
clearInterval(bla);
};
Mouse.addListener(asb2);
Now the event onMouseDrag will be called while the mouse is down and stopped calling when it is released. I'm using an object called asb2 as the mouse listener. Ok, now we need it to actually work - so that we can use it. Add:
asb.addListener(_root);
This will make the event be sent to the _root timeline (you can change that to any movieclip) so now we can do this:
_root.onMouseDrag = function () {
trace("[X: "+_xmouse+" | Y: "+_ymouse+"]");
};
Now it should call the function specified whenever the onMouseDrag event is broadcast. Here is the full code, and here is a working example, working albeit the fact that I haven't done proper Z sorting yet but that's unrelated.