A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [help] paste an array into input box

  1. #1
    Senior Member kendude's Avatar
    Join Date
    Sep 2003
    Location
    Hartford, CT USA
    Posts
    877

    [help] paste an array into input box

    What can I do to keep a 2d array as an array when I paste it into a user input text box, it keeps turning it into a string when I try to set the map variable to the variable assigned to the user input text box. I don't have my ASDG book with me to help either . This is to test a level after you create one for a tile based game. I'll bet the answer is something mad simple and I'm about to look like an arse. I've tried searching on this stuff, but the search here is just bogging, and google wasn't helping. Thank for all and any help.

  2. #2
    Senior Member kendude's Avatar
    Join Date
    Sep 2003
    Location
    Hartford, CT USA
    Posts
    877
    Thanks for the tip Mike. In the map editor (modified version of Klas') I output a 2d array in string form and now I also output a 1d array (as a string) to an output box so the user can copy that and paste it into the first screen of the level t to see if it works, if so they just email me the 2d array that was also outputted. Here is the code if anyone is interested:
    Take a string and make it into a 2d array (map is 18 x 18)
    code:

    map1 = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []];
    //
    // ------Turn a one dimensional array into a two dimensional array---->>
    make2D = function (o) {
    var cntx = 0;
    var cnty = 0;
    for (var i = 0; i<o.length; i++) {
    //trace(i);
    map1[cnty][cntx] = (o[i]);
    //trace(o[i]);
    trace(map1[cnty][cntx]);
    cntx++;
    if (cntx>17) {
    cntx = 0;
    cnty++;
    }
    }
    // trace(map1[0].length);
    // trace(map1.length);
    // trace(map1);
    };
    //
    tmpString = "7,12,14,14,14,14,14,14,14,14,14,14,14,14,14,14,13 ,7,7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7,5,10,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,7,7,12,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,7,7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7,7 ,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7,7,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,7,7,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,7,7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7,7,1,1,1 ,1,1,1,1,1,1,1,1,1,1,1,1,1,7,7,1,1,1,1,1,1,1,1,1,1 ,1,1,2,1,1,1,7,7,1,1,1,1,1,1,1,1,1,1,1,1,11,1,1,1, 7,7,1,1,1,1,1,1,1,1,1,1,1,11,7,1,1,1,7,7,1,1,1,1,1 ,1,1,1,1,1,1,7,7,1,1,1,7,7,3,1,1,1,1,1,1,1,1,1,11, 7,7,1,1,1,7,7,3,3,1,1,1,1,1,1,1,3,7,7,7,1,1,1,7,6, 9,9,9,9,9,9,9,9,9,9,4,7,6,9,9,9,4,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7";
    var convertedString = tmpString.split(",");
    make2D(convertedString);



    and this will turn the 2d map into a 1d array...
    code:

    map1 = [[7,12,14,14,14,14,14,14,14,14,14,14,14,14,14,14,13, 7],
    [7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7],
    [5,10,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7],
    [7,12,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,1,11,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,11,7,1,1,1,7],
    [7,1,1,1,1,1,1,1,1,1,1,1,7,7,1,1,1,7],
    [7,3,1,1,1,1,1,1,1,1,1,11,7,7,1,1,1,7],
    [7,3,3,1,1,1,1,1,1,1,3,7,7,7,1,1,1,7],
    [6,9,9,9,9,9,9,9,9,9,9,4,7,6,9,9,9,4],
    [7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7]];
    // 1d array
    //
    mapCondensed = new Array();
    // ------Turn a two dimensional array into a one dimensional array---->>
    make1D = function (arr) {
    var w = arr.length;
    var h = arr[0].length;
    for (var i = 0; i<w; i++) {
    for (var j = 0; j<h; j++) {
    // trace(arr[i][j]);
    mapCondensed.push(arr[i][j]);
    }
    }
    trace(mapCondensed);
    };
    make1D(map1);


    People searching for this might look for make a one dimensional array into a two dimensional array, etc...
    Last edited by kendude; 04-20-2005 at 04:34 PM.

  3. #3
    Untitled-2.fla
    Join Date
    Jul 2002
    Posts
    391
    sorry Ken, I deleted the last post, and was going to repost it again (I replied with the answer for a 1D array). I couldn't repost (the server was too busy)
    this is basically all you need
    Code:
    var levelString = "1,1,1,1,1,1,1,1|,2,3,41,1,2,4,1,2,3,5,1"
    var level = levelString.split ( "|" )
    for ( var i in level )
    	level[i] = level[i].split( ",")
    delete levelString	
    	
    var level1	= [ [ 1,2,3,4,1,2,4,2 ], [ 1,2,4,5,6,7,4,5,1,2 ] ]
    var level2  = new Array
    for ( var i in level1 )
    	level2[i] = level1[i].join ( "," )
    
    levelString1 = level2.join("|")
    delete level2
    personall, I'd have this saved to a database (or on your server as an xml file) - rather than e-mail

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center