Array Function Trouble

  • Hi, I'm having some trouble getting my functions to give me my desired output..

    // a quick example of a map
    // tile1 = ([1, 2, 3, "walk", 0.45, 10]);
    // tile2 = ([4, 5, 6, "wall", 0.23, 70]);
    // mymap = ([ ([tile1,tile1,tile2]),
    // ([tile2,tile2,tile2]) ]);
    var newMap:Boolean = true;
    var map:Array;
    function createMap(w, h, tile) {
    var map:Array;
    for (var i = 0; i < h; ++i) {
    map.push(createRow(w, tile));
    }
    return map;
    }
    function createRow(w, tile) {
    var row:Array;
    for (var i = 0; i < w; ++i) {
    row.push(tile);
    }
    return row;
    }
    function mapHandle(w, h, map, default_tile) {
    var newmap:Array;
    var mapW = map[0].length;
    var mapH = map.length;
    if (newMap) {
    newmap = createMap(w, h, default_tile);
    return newmap;
    }
    else {
    if (w < mapW) {
    for (var i = 0; i < mapH; ++i) {
    for (var j = 0; j < (mapW - w); ++j) {
    map[i].pop();
    }
    }
    }
    if (w > mapW) {
    for (var i = 0; i < mapH; ++i) {
    for (var j = 0; j < (w - mapW); ++j) {
    map[i].push(default_tile);
    }
    }
    }
    if (h < mapH) {
    for (var i = 0; i < (mapH - h); ++i) {
    map.pop();
    }
    }
    if (h > mapH) {
    for (var i = 0; i < (h - mapH); ++i) {
    map.push(createRow(w, default_tile));
    }
    }
    }
    }

    .. when I try to do something like this:
    var testTile = ([1,2,3,4,5]);
    var testMap = createMap(10,10,testTile);
    trace(testMap);

    I end up with undefined instead of my 10x10 3D array.

    I'm working on http://www.student.cs.uwaterloo.ca/~bbobnis/ if that's any aid at all.

    I want to get an array instead of undefined. What am I doing wrong?

    Thank you in advance,
    decumbo


  • not sure if this is it, but when you declare or creat an array, you shouldn't place the parens around your brackets:


    //you have:
    var title:Array = ([a,b,c]);
    //should just be
    var title:Array = [a,b,c];
    // unless I'm missing something here


  • var newMap:Boolean = true;
    var map:Array = Array();

    function createMap(w, h, tile) {
    var map:Array = Array();
    for (var i = 0; i < h; ++i) {
    map.push(createRow(w, tile));
    }
    return map;
    }

    function createRow(w, tile) {
    var row:Array = Array();
    for (var i = 0; i < w; ++i) {
    row.push(tile);
    }
    return row;
    }

    function mapHandle(w, h, map, default_tile) {
    var newmap:Array = Array();
    var mapW = map[0].length;
    var mapH = map.length;

    if (newMap) {
    newmap = createMap(w, h, default_tile);
    return newmap;
    }
    else {
    if (w < mapW) {
    for (var i = 0; i < mapH; ++i) {
    for (var j = 0; j < (mapW - w); ++j) {
    map[i].pop();
    }
    }
    }
    if (w > mapW) {
    for (var i = 0; i < mapH; ++i) {
    for (var j = 0; j < (w - mapW); ++j) {
    map[i].push(default_tile);
    }
    }
    }
    if (h < mapH) {
    for (var i = 0; i < (mapH - h); ++i) {
    map.pop();
    }
    }
    if (h > mapH) {
    for (var i = 0; i < (h - mapH); ++i) {
    map.push(createRow(w, default_tile));
    }
    }
    }
    }

    The following test cases work now:
    var testTile:Array = Array(1,2,3);
    var testMap:Array = createMap(1,2,testTile);
    trace(testMap);
    trace(testMap[1][0][2]);

    Thought I'd post this in case anyone else was having similar problems.

    I'll bug this forum when I get stumped, again,
    decumbo







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about Array Function Trouble , Please add it free.