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; i<arguments.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; i<ob_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: [email protected]
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; i<ob_ar.length; i++) {
                        ob_ar[i]._x = targetX+(ob1._width/2);
                        ob_ar[i]._y = targetY+(ob1._width/2);
                }
        }

center(myMC, [myMC2, myMC3, myMC4]);



_______________________________________________
[email protected]
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


_______________________________________________
[email protected]
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



_______________________________________________
[email protected]
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

Reply via email to