RE: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters
I think your code won't center correctly does it? Don't know if [] syntax is slower than creating a local var fisrt, but just in case added it. function center(ob1:MovieClip, ob_ar:Array) { var x:Number = ob1._x - (ob1._width/2); var y:Number = ob1._y; for (var i = 0; iob_ar.length; i++) { var mc : MovieClip = on_ar[i]; mc._x = x + (mc._width/2); mc._y = y + (mc._width/2); } } Top of my head, didn't test it. Ben Smeets -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Alistair Colling Sent: vrijdag 11 mei 2007 13:41 To: flashcoders@chattyfig.figleaf.com Subject: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters Hi there, just a quick syntax question, I want to receive a function that will centre the position of all objects passed to that of the first object passed but the number of objects passed may vary. This is the syntax I am currently using, is this the best way to do it? Thanks for any comments in advance :) Ali //ob1 is the object that the objects in the array centre themselves to public static function center(ob1:Object, ob_ar:Array) { var targetX:Number = ob1._x; var targetY:Number = ob1._y; for (var i = 0; iob_ar.length; i++) { ob_ar[i]._x = targetX+(ob1._width/2); ob_ar[i]._y = targetY+(ob1._width/2); } } center(myMC, [myMC2, myMC3, myMC4]); ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you. ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters
The parameters received by a function are automatically stored in a built-in array called arguments. So you can do something like this: public static function center(obj1:Object) { var targetX:Number = ob1._x; var targetY:Number = ob1._y; for (var i = 1; iob_ar.length; i++) { arguments[i]._x = targetX+(ob1._width/2); arguments[i]._y = targetY+(ob1._width/2); } } } Btw, obj1 doesn't have to be passed seperately either - you can use var targetX:Number = arguments[0]._x; - but there's no reason to do so in your code, and this way the method is a bit more clear. The only caveat is that, depending on the rest of your code, if you have to pass an unknown number of movie clips, there's a chance your original function might actually make more sense... Cheers, Karina -Original Message- From: Alistair Colling [mailto:[EMAIL PROTECTED] Sent: 11 May 2007 12:41 To: flashcoders@chattyfig.figleaf.com Subject: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters Hi there, just a quick syntax question, I want to receive a function that will centre the position of all objects passed to that of the first object passed but the number of objects passed may vary. This is the syntax I am currently using, is this the best way to do it? Thanks for any comments in advance :) Ali //ob1 is the object that the objects in the array centre themselves to public static function center(ob1:Object, ob_ar:Array) { var targetX:Number = ob1._x; var targetY:Number = ob1._y; for (var i = 0; iob_ar.length; i++) { ob_ar[i]._x = targetX+(ob1._width/2); ob_ar[i]._y = targetY+(ob1._width/2); } } center(myMC, [myMC2, myMC3, myMC4]); ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters
Thanks Karina this is the sort of thing I was looking for, as you say it may be clearer to code this fn the way I initially said but I was interested in how to code this another way so thanks for helping me. One tiny typo, before someone else does- for (var i = 1; iarguments.length; i++) Thanks again :) Ben, thanks for your help too, I hadn't thought of only using the square brackets to make the code more efficient. Ali On 11 May 2007, at 15:32, Karina Steffens wrote: The parameters received by a function are automatically stored in a built-in array called arguments. So you can do something like this: public static function center(obj1:Object) { var targetX:Number = ob1._x; var targetY:Number = ob1._y; for (var i = 1; iob_ar.length; i++) { arguments[i]._x = targetX+(ob1._width/2); arguments[i]._y = targetY+(ob1._width/2); } } } Btw, obj1 doesn't have to be passed seperately either - you can use var targetX:Number = arguments[0]._x; - but there's no reason to do so in your code, and this way the method is a bit more clear. The only caveat is that, depending on the rest of your code, if you have to pass an unknown number of movie clips, there's a chance your original function might actually make more sense... Cheers, Karina -Original Message- From: Alistair Colling [mailto:[EMAIL PROTECTED] Sent: 11 May 2007 12:41 To: flashcoders@chattyfig.figleaf.com Subject: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters Hi there, just a quick syntax question, I want to receive a function that will centre the position of all objects passed to that of the first object passed but the number of objects passed may vary. This is the syntax I am currently using, is this the best way to do it? Thanks for any comments in advance :) Ali //ob1 is the object that the objects in the array centre themselves to public static function center(ob1:Object, ob_ar:Array) { var targetX:Number = ob1._x; var targetY:Number = ob1._y; for (var i = 0; iob_ar.length; i++) { ob_ar[i]._x = targetX+(ob1._width/2); ob_ar[i]._y = targetY+(ob1._width/2); } } center(myMC, [myMC2, myMC3, myMC4]); ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters
i-- will eventually become 0 which (in AS2) is evaluated to false. so while(i--) works fine. regards, Muzak - Original Message - From: eka [EMAIL PROTECTED] To: flashcoders@chattyfig.figleaf.com Sent: Friday, May 11, 2007 6:33 PM Subject: Re: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters Hello :) you don't limit your while loop instruction ? while ( --i -1) { } EKA+ :) ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters
I think he was pointing out how the loop could be optimized even further. Pre-decrementing is faster than post-decrementing. However, his greater than comparison slows it down. The right way to do it is: while (--i -(-1)) { } since subtraction is faster than addition. This is all academic, though. I used to use --i -(-1) but I gave it up for the readability of while (i--). This is my new favorite: while (i--) i++; ;) ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Quick syntax q: how to write a function that canreceive an undefined number of parameters
Hello :) Yes it's funny .. i forget the 0 is a boolean in a conditional instruction... the problem with this loop is the negative values. but it's a good notation. After tests... in AS2 this method is more speed. var ar:Array = new Array(50) ; var time ; Key.addListener(this) ; onKeyDown = function() { var result ; var code:Number = Key.getCode() ; var copy:Array = [].concat(ar) var len:Number = copy.length ; switch (code) { case Key.UP : { time = getTimer() ; for( var i:Number = 0 ; ilen ; i++ ) { result = copy[len] ; } time = getTimer() - time ; trace( for speed : + time + ms) ; break ; } case Key.DOWN : { time = getTimer() ; while( len-- ) { result = copy[len] ; } time = getTimer() - time ; trace( while(len--) speed : + time + ms) ; break ; } case Key.LEFT : { time = getTimer() ; while( --len -1 ) { result = copy[len] ; } time = getTimer() - time ; trace( while(--len -1) speed : + time + ms) ; break ; } case Key.RIGHT : { time = getTimer() ; while( --len -(-1) ) { result = copy[len] ; } time = getTimer() - time ; trace( while(--len -(-1)) speed : + time + ms) ; break ; } } } for speed : 329 ms while(len--) speed : 305 ms while(--len -1) speed : 324 ms while(--len -(-1)) speed : 418 ms Thanks :) EKA+ :) 2007/5/11, Steven Sacks [EMAIL PROTECTED]: I think he was pointing out how the loop could be optimized even further. Pre-decrementing is faster than post-decrementing. However, his greater than comparison slows it down. The right way to do it is: while (--i -(-1)) { } since subtraction is faster than addition. This is all academic, though. I used to use --i -(-1) but I gave it up for the readability of while (i--). This is my new favorite: while (i--) i++; ;) ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com