-
Ternaries on Mouse interaction Y/N?
Pros and cons of doing this please:
Actionscript Code:
(e.type == "rollOver")? doOver:doOut;
Any gain except of condensing the view?
Any loss on speed or performance?
Thanx
-
Senior Member
http://en.wikipedia.org/wiki/Program...en_to_optimize more or less says it all.
When in doubt, learn to write for others reading your code (this will come in handy for them, or for when you want to come back and update your code). What good is something that goes ~1% faster if no one can figure out what it originally meant to do.
For my eyes, each listened event having its own event handler would be easier to read; In contrast with a 'one size fits all' routing function (in your case, switching between doOver, and doOut)--- im guessing that would be used like: 'addeventltnr(mouseevnt.over,SuperFunction); addeventltnr(mouseevnt.out, SuperFunction);'
Also if you have them in different functions, you will be able to quickly see what is being run where. ohhh the horrors I have seen/done; every other line 'if (thisfunctionisforthis)' do stuff, then the next 'if(thisfunctionisforsomethingelse)' do other stuff; oh the humanity! This makes updating, fixing or frankly just writing it near impossible.
So, in short: Program for the fastest Human Understanding, Not the fastest Computer Understanding. I suggest against the way you have posted.
Last edited by guardiankitty; 12-18-2011 at 10:35 PM.
-
I see.
Your suggestion feels right, its more clearly and easier to maintain.
Its a point and click game,I just have many many MouseEvents, code is getting huge, and I started to care for optimization.I thought to cut down the number of function blocks in it, but I wasnt sure if this example would do any relief on performance..
So, shortening the lines this way doesnt really makes the code noticeably faster,right?
-
Senior Member
Originally Posted by duchman
I see.
Your suggestion feels right, its more clearly and easier to maintain.
Its a point and click game,I just have many many MouseEvents, code is getting huge, and I started to care for optimization.I thought to cut down the number of function blocks in it, but I wasnt sure if this example would do any relief on performance..
So, shortening the lines this way doesnt really makes the code noticeably faster,right?
sorry for the wait on the answer: The short answer is test it and see. If it is a time sensitive operation (and the game seems sluggish), then spend a good deal of time in testing it both ways and see if its faster.
If you are doing a lot of event stuff, I can say without a doubt:
this is faster
Actionscript Code:
function MouseClick_Fire(e:MouseEvent):void{/*code*/} function MouseClick_Water(e:MouseEvent):void{/*code*/}
than
Actionscript Code:
function MouseClick_OneSizeFitsAll(e:MouseEvent):void{
if(e.currentTarget.isFireType){ /*code*/ return; }
if(e.currentTarget.isWaterType){ /*code*/ return; }
if(logicFunction()){ /*code*/ return; }
}
Processers can pre-cache results of conditional logic, (so for instance, if(true){/*code*/} will get cached pretty quickly, and each time it runs it will just assume that the condition is going to be the same each time.
However, stuff that shifts around a bunch takes longer to figure out- there is no real good way for the proc to guess what it is going to be. for instance:
var b:Bool = true;//init
if((b = !b)) {/*code*/}//per frame
The first time it is run, it would be false, then the next time true, then false, etc. The proc can not cache this result and it takes a logic check each time.
The moral of the story, is in place of 'conditioning' an event handeler- you should just make a new event handeler per event IF IT IS TIME SENSITIVE and optimization is required.
Now that is not to say that you should not listen to the sender of the event- actually the opposite, when if doubt this is much better:
Actionscript Code:
function MouseClickPlayer(e:MouseEvent){ e.currentTarget.play(); }
mc_1.addEventListener(MouseEvent.CLICK, MouseClickPlayer); mc_2.addEventListener(MouseEvent.CLICK, MouseClickPlayer); mc_3.addEventListener(MouseEvent.CLICK, MouseClickPlayer); mc_4.addEventListener(MouseEvent.CLICK, MouseClickPlayer);
than
Actionscript Code:
function MouseClickPlayer1(e:MouseEvent){ mc_1.play(); }
function MouseClickPlayer2(e:MouseEvent){ mc_2.play(); } function MouseClickPlayer3(e:MouseEvent){ mc_3.play(); } function MouseClickPlayer4(e:MouseEvent){ mc_4.play(); }
mc_1.addEventListener(MouseEvent.CLICK, MouseClickPlayer1); mc_2.addEventListener(MouseEvent.CLICK, MouseClickPlayer2); mc_3.addEventListener(MouseEvent.CLICK, MouseClickPlayer3); mc_4.addEventListener(MouseEvent.CLICK, MouseClickPlayer4);
The latter way is hard to work with, let alone is a pain to update. You are not really going to run the second way any faster, if anything I think they both will run about the same.
Another tip would be to save the e.currentTarget above to a variable if you are going to use it a bunch: ie- var mc:MovieClip = e.currentTarget; mc.play(); mc.x++;mc.y++; etc... This is much faster than referencing e.currentTarget each time.
Hope that is of some help; the long and the short of it is: Test if Necessary!
Best of luck!
-GK >^.^<
-
Thank You very much for Your time and tuts, its quite helpful!
Theres just so many ways to write a line of as3, and its good to have some
healthy advices.
Thnx Gk!
Last edited by duchman; 12-29-2011 at 02:29 PM.
Reason: addressing
-
Senior Member
NP at all, happy coding =3
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|