Recently while working on an as3 project the project manager of the project came over to me and asked that something happen automatically that was currently happening only on a mouse click. Normally I would have said fuck you that is impossible and get the hell out of my face. I was feeling confident today though with the event model of as3 so I said yeah I think we can make that happen. The logic was that at worst I would have to make a separate function based on the one I already had. So in a quest to solve this problem I offer you the most awesome kick ass thing you could ask for.
With as3 you quite literally fake an event. And you can do so very easily. All you need to do is import the events classes:
Then any time you have a button you can fake whatever event you want. You can fake all sorts of events not just button. But faking a click is often the most useful. The code for doing such a thing is as follows.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| // note you must make a symbol and link it in order to make an instance.
function init(){
mybutton = new customSymbolIHaveLinked();
mybutton.mouseChildren = false;
mybutton.mouseEnabled = true;
// this makes the button listen for the click event
mybutton.addEventListener(MouseEvent.CLICK, functiontorun);
// now if you would like to automatially run this event on start up to perhaps ensure the user is at a default
// position you simply need to dispatch an event
mybutton.dispatchEvent(new Event(MouseEvent.CLICK));
}
function functiontorun(evt:Event){
trace("recieved event from "+evt.target);
} |
That’s all there is to it really. I hope this has been helpful. It has been a lifesaver for me. Thanks again as3 for making life a hell of a lot easier…