Igli Kadija
Igli Kadija

Reputation: 105

As3 - Function with unlimited arguments as Movieclip?

I want to pass to a function an uncertain number of movieclips, sth like this

    function Test(...args)
    {
        for(var item in args){
             item.x = 100;
        }
    }

But using this method didn't work, any solution?

Upvotes: 0

Views: 193

Answers (2)

Marty
Marty

Reputation: 39456

You're almost there, you just need to use a for each loop for your example to work correctly:

function Test(...args)
{
    for each(var item:MovieClip in args)
    //  ^^^^
    {
         item.x = 100;
    }
}

Better however would be to accept an Array or Vector holding the MovieClips. This will greatly improve readability of your code later on:

function Test(list:Vector.<MovieClip>)
{
    for each(var item:MovieClip in list)
    {
         item.x = 100;
    }
}

Upvotes: 4

TML
TML

Reputation: 12966

Use arguments; see the Adobe Reference docs (for AS3), the MDN (for JS) or this example jsfiddle (for a working example).

[NB: question originally tagged as JS, leaving javascript/jsfiddle in there for reference]

Upvotes: 0

Related Questions