Search code examples
actionscript-3flashactionscriptflash-cs6

Executing a function for all instances of a class?AS3


I'm really new to AS3 and I was wondering how I can apply a function to all instances of a class I'll show you guys what I have, is there anything I can do?

var cloud:Cloud;
for (i=0; i<5; i++)
{
    cloud = new Cloud();
    addChild(cloud);
}

this makes many instances of the Cloud class. Later on I want to execute a function on all of these instances at once, how would I do that?


Solution

  • Store them in an Array or Vector:

    var clouds:Vector.<Cloud> = new <Cloud>[];
    
    for(var i:int = 0; i < 5; i++)
    {
        var cloud:Cloud = new Cloud();
    
        clouds.push(cloud);
        addChild(cloud);
    }
    

    Then iterate over that list and call a function on each item:

    for each(var cloud:Cloud in clouds)
    {
        cloud.do();
    }