0

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 stage, 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.

1
  • If you ever get a error, post the exact error message. Commented Oct 5, 2011 at 3:21

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

8 Comments

so first off thank you very much for posting an answer to my question. I attempted to implement what you have given me, but i keep getting this error. 1067: Implicit coercion of a value of type Array to an unrelated type Class. here is how i'm calling this method in my main .as file. Blocks = MyBase.findObjects(Block,gamesprite); // block is the name of the object on the stage. and this function works // if i don't have another instance of it. turret = MyBase.findObjects(turret, gamesprite);
what if i defined the var out:Array to be type Class instead? can you do that?
That error suggests that you're parsing a variable which is of type Array as the first argument of findObjects()
And no, out is an Array that is created, filled and then returned by findObjects()
I think the only way it is of type array is if i call that method more than once, because then it adds two items to check for. that's just a thought.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.