开发者

Creating a new array for eache iteration of the function call. AS3

开发者 https://www.devze.com 2023-04-10 12:36 出处:网络
so I have writing a function that returns objects on the stage and puts them into an array. and the function works fine until i call the function on more than one object name, meaning if im in the roo

so I have writing a function that returns objects on the stage and puts them into an array. and the function works fine until i call the function on more than one object name, meaning if im in the root class, and I call this function on object1 lets say it will add all the object one's from the sta开发者_运维问答ge, but if i call it on object2 it will throw an error, which makes some sense, i guess it means that it is not adding it to a unique array, but im not sure how to do that.

would it be a good idea to maybe make a multidimensional array? if that is the case would it be too slow?

here is the function code:

public function findObjects(objectName, objLocation, bVisible = false):Array{
    for (var i = 0; i < objLocation.numChildren; i++){
        var nObj=objLocation.getChildAt(i);
        if (nObj is objectName){
            // add to array and make invisible
            obj.push(nObj);
            nObj.visible=bVisible;
        }
    }
    return obj;
}

any help with this would be greatly appreciated.


Try this:

public function findObjects(type:Class, target:DisplayObjectContainer, bVisible:Boolean=false):Array
{
    var out:Array = [];

    for(var i:int = 0; i<target.numChildren; i++)
    {
        var obj:DisplayObject = target.getChildAt(i);

        if(obj is type)
        {
            out.push(obj);
            obj.visible = bVisible;
        }
    }

    return out;
}

And then based on your code, the implementation would probably be:

obj = findObjects(MovieClip, container);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号