Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Load a 2D array from a txt file

New Here ,
Nov 11, 2014 Nov 11, 2014

Hi, i have a little problem about array :/,

I'm coding a little tile based game, and i load the map from a .txt file.

The txt file is like that :

0 1 0 1 0 1 0 0 0 0 0 1 1 1 1

1 0 1 0 1 0 0 1 1 0 1 0 1 0 1

...

and i just want to use it in this loop :

for (var i:int = 0; i < mapHeight; i++)

{

         for (var j:int = 0; j < mapWidth; j++)

         {

         var cell:MovieClip = new tile();

         cell.gotoAndStop(map +1);

                 if (i % 2 == 1) {

                      cell.x=70*j  -35;

                      cell.y=35*i;

                 }

                 else{

                      cell.x=70*j;

                      cell.y=35*i;

                 }

addChild(cell);

         }

}

This loop works if i create the map array in the AS3 file but with 10 maps the code will be very very very long.

I have tried to load the array from a txt file but there is not a lof information on internet and i'm blocked.

For load the txt file :

var mapLoader:URLLoader = new URLLoader();

mapLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event):void {

          var maptext:String = e.target.data;

          var map:Array = maptext.split(" ");

          trace(map);

}

mapLoader.load(new URLRequest("map1.txt"));

But i don't understand how to use "map" after this, i can't use it in the loop for add cell, i have an error.

I find all this stuff really complicated for just load a simple 2D array from a txt file.

Can't i just put every map in AS3 in a different txt file and load this part of code when i need it ? Like that :

"var map:Array = [

  [1,0,0,1],

  [1,1,1,1],

  [0,0,0,0],

  [0,1,0,1],

]; "

Thanks for the help and sorry for the bad english.

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 11, 2014 Nov 11, 2014

If I recreate the first version I get the same result as what you show for the second version - I do not get the broken up result you show.  My guess is that you have the data file prepared improperly when you arrange the data in it.

Translate
LEGEND ,
Nov 11, 2014 Nov 11, 2014

When you parse the text data you need to create the 2D array by first isolating the outer array using the line breaks, and then process each of the inner arrays from each element of that array.  The traces in the code below are only to help you see how it breaks down

var mapLoader:URLLoader = new URLLoader();
var map:Array;  // declare outside the function if you want it available outside the function

mapLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event):void {

          var maptext:String = e.target.data;

          map = maptext.split("\r\n");  // to create the outer array (i loop/mapHeight)

 

          trace("number of rows: "+map.length);

          for(var i:int=0; i<map.length; i++){
               map = map.split(" ");  // to create the inner arrays (j loop/mapWidth)
               trace(i+":  ",map);
          }
}

mapLoader.load(new URLRequest("map1.txt"));

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 11, 2014 Nov 11, 2014

Thanks now, i "understand" the function and how it works.

But i'm still lost ahaha, i don't understand why "trace(map)" return "null" 😕 .

Wait a second, i'll make some test and find why, i need to search by myself ahaha .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 11, 2014 Nov 11, 2014

If you are trying to trace it before it is assigned any value then it will trace a null.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 11, 2014 Nov 11, 2014

It's weird ahaha, i have created 2 projet : 1 with the array in the code and 1 with your code.

When i use trace(map) on both i have the same result, wich is great.

but the result is different when the array goes in the cell loop (here i'm lost).

Maybe because i don't have understand the place where i should but the cell loop.

var mapLoader:URLLoader = new URLLoader();

var map:Array;  // declare outside the function if you want it available outside the function

mapLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event):void {

          var maptext:String = e.target.data;

          map = maptext.split("\r\n");  // to create the outer array (i loop/mapHeight)

         // trace("number of rows: "+map.length);

          for(var i:int=0; i<map.length; i++){

               map = map.split(" ");  // to create the inner arrays (j loop/mapWidth)

               //trace(i+":  ",map);

          }

trace(map)  <=== this trace return the good array

Should i put the cell code here ? Or i'm all wrong ?

}

mapLoader.load(new URLRequest("map1.txt"));

trace(map); <=== this trace return null

Thanks for taking time for responding, you illuminate my day.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 11, 2014 Nov 11, 2014

Code does not stand in line waiting to execute.  Whatever can process immediately will do that.  The line where you say it traces null will execute long before the function that loads the data will.

While you should put the cell code where you say "Should i put the cell code here ? Or i'm all wrong ?", it would be cleaner to just have the cell code in a sepatrate function and then call that function from that spot.... after the map array has been filled.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 11, 2014 Nov 11, 2014

So this code should be good ?

I don't understand what you mean by saying " after the map array has been filled".

var mapLoader:URLLoader = new URLLoader();

var map:Array;  // declare outside the function if you want it available outside the function

function boucleCell(map):void

{

  for (var i:int = 0; i < mapHeight; i++)

  {

  for (var j:int = 0; j < mapWidth; j++)

  {

  var cell:MovieClip = new tile();

  cell.gotoAndStop(map +1);

  if (i % 2 == 1) {

  cell.x=70*j  -35;

  cell.y=35*i;

  }

  else{

  cell.x=70*j;

  cell.y=35*i;

  }

  addChild(cell);

  }

  }

};

mapLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event):void {

          var maptext:String = e.target.data;

          map = maptext.split("\r\n");  // to create the outer array (i loop/mapHeight)

         // trace("number of rows: "+map.length);

          for(var i:int=0; i<map.length; i++){

               map = map.split(" ");  // to create the inner arrays (j loop/mapWidth)

               //trace(i+":  ",map);

          }

  trace(map);

boucleCell(map);

}

mapLoader.load(new URLRequest("map1.txt"));

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 11, 2014 Nov 11, 2014

Yes it should work, though you do not have to pass the map variable in as long as you have it declared outside the function.  If you wish to pass it in then you can declare it inside the function lke you had originally.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 11, 2014 Nov 11, 2014

Too bad it's not working ahah, i'm searching for more than 1 hour why and i don't understand 😕

So this is the code made with your help :

import flash.display.MovieClip;

// Start

var mapWidth = 15;

var mapHeight = 15;

var tileSide = 70;

var totalTiles = mapWidth * mapHeight;

var mapLoader:URLLoader = new URLLoader();

var map:Array;  // declare outside the function if you want it available outside the function

function boucleCell():void

{

  for (var i:int = 0; i < mapHeight; i++)

{

    for (var j:int = 0; j < mapWidth; j++)

    {

  var cell:MovieClip = new tile();

  cell.gotoAndStop(map +1);

  if (i % 2 == 1) {

  cell.x=70*j  -35;

  cell.y=35*i;

  }

  else{

  cell.x=70*j;

  cell.y=35*i;

  }

  addChild(cell);

    }

}

};

mapLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event):void {

          var maptext:String = e.target.data;

          map = maptext.split("\r\n");  // to create the outer array (i loop/mapHeight)

         // trace("number of rows: "+map.length);

          for(var i:int=0; i<map.length; i++){

               map = map.split(" ");  // to create the inner arrays (j loop/mapWidth)

               //trace(i+":  ",map);

          }

  trace(map);

boucleCell();

}

mapLoader.load(new URLRequest("map1.txt"));

The result :

bugeed.png

With the array in the AS3 :

import flash.display.MovieClip;

// Start

var mapWidth = 15;

var mapHeight = 15;

var tileSide = 70;

var totalTiles = mapWidth * mapHeight;

//Array in the code

var map:Array = [

  [0,1,0,1,0,1,0,0,0,0,0,1,1,1,1],

  [1,0,1,0,1,0,0,1,1,0,1,0,1,0,1],

  [1,1,1,1,1,1,1,1,1,1,1,1,1,0,1],

  [0,0,0,0,0,0,0,0,0,0,1,0,0,0,1],

];

trace(map)

for (var i:int = 0; i < mapHeight; i++)

{

    for (var j:int = 0; j < mapWidth; j++)

    {

  var cell:MovieClip = new tile();

  cell.gotoAndStop(map +1);

  if (i % 2 == 1) {

  cell.x=70*j  -35;

  cell.y=35*i;

  }

  else{

  cell.x=70*j;

  cell.y=35*i;

  }

  addChild(cell);

    }

}

Result : fine.png

But both trace(map) are the same. ><"

Again, thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 11, 2014 Nov 11, 2014

If I recreate the first version I get the same result as what you show for the second version - I do not get the broken up result you show.  My guess is that you have the data file prepared improperly when you arrange the data in it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 11, 2014 Nov 11, 2014

Thanks, i'm stupid to not have think about it.

I've created the exact same projet from 0, with just 1 copy past from the old code and everything works.

This thing/bug already happened to me with an other game, i should have predict it ^^.

Thanks for the help, have a good day/night.

I'm really happy that people with your level help beginner, you're a good person !

PS:Ahaha, i'm sorry again, but with the new project it's almost impossible to test, everything bug when i press crtl + enter, same in google chrome.Oh god.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 11, 2014 Nov 11, 2014

Yeah it's like flash can show the map for 3seconds and after that the pluggin crash

Like there was an inifite loop (chrome take 40% of UC for just show 225 tiles.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 11, 2014 Nov 11, 2014
LATEST

It's ok i have found the problem !

Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines