I've gotten a lot of positive feedback on the Cool Flash Tip of the Week series. So as long as people find them useful, I'll keep them coming. Make sure to subscribe to be on top of all tips and add me on twitter. Now with all the formalities out the way, lets get a move on to the tip of the week. This week's tip is about Events!!
I'm sure you are wondering, what kind of tips can you not know already. Events are a big topic in Actionscript 3.0. Almost everything is based on event listeners and is event driven. During my time coding, I found a couple of handy shortcuts that made my day easier. Let's say you have a simple event handler that adds a MovieClip to the stage when a link is clicked. For simplicity's sake , lets pretend we have a MovieClip in the library named "myMC", and a button on the stage called "myLink".
myLink.addEventListener(MouseEvent.CLICK, addClip);
function addClip(e:MouseEvent):void
{
stage.addChild(myClip);
}
This would be the typical way we would do it. This is absolutely correct, but what if we wanted in a different function to add the clip as well. Some one would probably try to execute that this way.
myLink.addEventListener(MouseEvent.CLICK, addClip);
otherFunction();
function addClip(e:MouseEvent):void
{
stage.addChild(myClip);
}
function otherFunction():void
{
stage.addChild(myClip);
}
Although this is correct, the ideal way to program is to reuse code as much as possible so that when its time to update you caan change the code in one spot and it will update throughout the program. Not to mention it cleans up the code considerably. A better alternative to this would be to call addClip(). But WAIT!!, this is an event handler, if I call this I get an Argument Error. This is true, but we can setup the event handler to be an event listener and also be called without needing an event. Here is how we acheive that.
myLink.addEventListener(MouseEvent.CLICK, addClip);
addClip();
function addClip(e:MouseEvent = null):void
{
stage.addChild(myClip);
}
//------------------OR--------------------
myLink.addEventListener(MouseEvent.CLICK, addClip);
addClip(null);
function addClip(e:MouseEvent):void
{
stage.addChild(myClip);
}
That was pretty simple now was it. I use this all the time in my code to shorten things up and clean my code. I do prefer to the first method than the second alternative. I rather not have to keep passing null than just setting the default to null instead. Keep in mind, that this will only work if you do not need the actual event in your functions logic. If you do, then you need to an event. Well that's all for this week's tip. I hope you found it useful in any sense. Next week will be a big week, so stay tuned and subscribe to the feed.
Great tip Clemente! I use this all the time. Another addition is that you can use it for multiple parameters...
ReplyDeletefunction doSomething(a:int=null,b:int=null){...}
And if you have parameters which are required, you'll have to place them before any optional parameters...
function doSomething(required:int,optional:int=null){...}
Can you give a better example of how to combine that with events, because i don't fully understand. And it could help me.
ReplyDeleteAnd by the way, keep up the good work KreativeKING!
wow !! thank you and keep up the good work Clemente ..
ReplyDeleteoh yah , i liked the new site design..
yusuf
For example, if you want to do something based on the target of an event, or you just want to send the target in directly you could do the following...
ReplyDeletefunction doSomething(e:Event=null,target:Object):void
{
if (e != null)
target = e.target;
...
}
@Chris, well no, because remember it is still an Event handler, so you would most likely need to initialize that target:Object = null so you don't run into any problems when just firing out the event.
ReplyDelete[...] DIRECT LINK » [...]
ReplyDelete[...] I’ve gotten a lot of positive feedback on the Cool Flash Tip of the Week series. So as long as people find them useful, I’ll keep them coming. Make sure to subscribe to be on top of all tips and add me on twitter. Now with all the formalities out the way, lets get a move on to the tip of the week. This week’s tip is about Events!! DIRECT LINK » [...]
ReplyDeleteOne other thing I do fairly often is that I will pass a new event that reflects what's going on. For example:
ReplyDelete[code]
myLink.addEventListener(MouseEvent.CLICK, addClip);
addClip(new Event("oneTrigger",true));
addClip(new Event("twoTrigger",true));
function addClip(e:Event):void
{
stage.addChild(myClip);
}
[/code]
While this method involves more typing, you can trace() event targets within your functions and figure out exactly what event is triggering the function. I find this helpful in larger, more complex applications.
Great tip though - keep 'em coming!
Nice tip Clemente. Thanks for the advice.
ReplyDeleteI have one other approach that I use. Simply design a generic function that adds a clip. Then use the event listener to call that function. The benefit is that the generic function can keep track of whether or not the clip is already visible and thus ignore requests to run the clip again.
Thanks again, and keep the tips coming!!!
Dan
I did not know about it. Thanks
ReplyDeleteVery interesting. Thanks. I like it you blog :)
ReplyDeleteSweet, keep it up. I really like this post...
ReplyDeleteOne other thing I do fairly often is that I will pass a new event that reflects what's going on. For example:
ReplyDelete[code]
myLink.addEventListener(MouseEvent.CLICK, addClip);
addClip(new Event("oneTrigger",true));
addClip(new Event("twoTrigger",true));
function addClip(e:Event):void
{
stage.addChild(myClip);
}
[/code]
While this method involves more typing, you can trace() event targets within your functions and figure out exactly what event is triggering the function. I find this helpful in larger, more complex applications.
Great tip though - keep 'em coming!