Search code examples
actionscript-3actionscriptflash-cc

Can't create array of arrays


I'm trying to create array of arrays (like a 2d array) however I'm getting error:

TypeError: Error #1006: value is not a function.

Here's my code:

I'm using Flash Professional CC 2015. How can I fix this error?

EDIT: Here's the full function:

function CreateMainMenu(xPos:Number, yPos:Number, depth:int, menu_xml:XML):void {
    // Generate menu list
    var arr:Array = new Array();
    addChild(mainmenu_mc);

    mainmenu_mc.x = xPos;
    mainmenu_mc.y = yPos;
    setChildIndex(mainmenu_mc, depth);

    var num:int = 0;
    for each (var tempNode:XML in menu_xml.elements()) {
        var arr2:Array = new Array();
        arr2.push(tempNode);
        arr2.push("menu");
        arr[num].push(arr2); // It gives error
        num++;
    }

    trace (arr);

    // GenerateMenu(this, "mainmenu_mc", xPos, yPos, depth, arr);
}

The first line number is 58, the last one is 79.

I'm getting this error:

TypeError: Error #1010: A term is undefined and has no properties. at xmlmenu_05_fla::MainTimeline/CreateMainMenu()[xmlmenu_05_fla.MainTimeline::frame1:72] at xmlmenu_05_fla::MainTimeline/processXML()[xmlmenu_05_fla.MainTimeline::frame1:118] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at flash.net::URLLoader/onComplete()


Solution

  • The problem is that you never add anything to arr.

    You create the Array here:

    var arr:Array = new Array();
    

    but the only time you interact with it is the line that gives you the error:

    arr[num].push(arr2); // It gives error
    

    You are trying to access an element here, but you never added anything to the array.

    Your variables names are not very descriptive and you likely got lost in this mess:

    var arr2:Array = new Array();
    arr2.push(tempNode);
    arr2.push("menu");
    arr[num].push(arr2); // It gives error
    num++;
    

    I cannot tell what your intentions are here. IF you just want to add arr2 as the next element, use push, there's no need for num here.

    If you write code with meaningful variables names it's easier to keep track of your own code.