Search code examples
actionscript-3flashclassaddchild

Actionsript 3.0 control multiple Instances of a class using a single function


Hey I'm new to Flash AS 3.0 and I am having trouble with creating instances of classes and want to Control them using a single function for all of them.

import flash.display.MovieClip;
import flash.events.*;
stage.addEventListener (KeyboardEvent.KEY_DOWN, movestuff);

var newsymbol:MovieClip;
newsymbol = new Symbol1;
addChild(newsymbol);
newsymbol.x = 200
newsymbol.y = 200
addChild(newsymbol);

function movestuff (event:KeyboardEvent):void
{

newsymbol.x + 100
}

Symbol1 is a class from the library that I am trying to move, and I want to add multiple instances of it but control all of them using the function movestuff


Solution

  • Keep the instances in an array:

    var penguinArray:Array = [];
    
    function addPenguin(){
        var newPenguin:Penguin = new Penguin();
        //do stuff
        penguinArray.push(newPenguin);
    }
    
    function moveStuff(){
        for(var i in penguinArray){
            penguinArray[i].x += 10;
        }
    }