Jump to content

Create tilemap from an Array


legnox
 Share

Recommended Posts

i think for the array you could do something like - 

this.tile_array = 	[				[0,0,0,0,0,0,0,0,0],				[0,1,1,1,0,0,0,0,0],				[0,0,0,0,0,0,1,1,1],				[0,2,0,0,0,0,0,0,0],				[1,1,1,1,1,1,1,1,1],			];

and then you could write a for loop like this - 

this.floor_group = this.add.group(0,0);for(var i = 0; i<9; i++) {	for(var j = 0; j<15; j++) {		if(this.tile_array[i][j] == 1) {			var floor = this.floor_group.create(i*20,j*20, 'floor');			// add immovable or something here		} else if(this.tile_array[i][j] == 2) {			this.player = this.add.sprite(i*20,j*20, 'player');		} // add more else ifs for more items in your array	}}
Link to comment
Share on other sites

 

i think for the array you could do something like - 

this.tile_array = 	[				[0,0,0,0,0,0,0,0,0],				[0,1,1,1,0,0,0,0,0],				[0,0,0,0,0,0,1,1,1],				[0,2,0,0,0,0,0,0,0],				[1,1,1,1,1,1,1,1,1],			];

and then you could write a for loop like this - 

this.floor_group = this.add.group(0,0);for(var i = 0; i<9; i++) {	for(var j = 0; j<15; j++) {		if(this.tile_array[i][j] == 1) {			var floor = this.floor_group.create(i*20,j*20, 'floor');			// add immovable or something here		} else if(this.tile_array[i][j] == 2) {			this.player = this.add.sprite(i*20,j*20, 'player');		} // add more else ifs for more items in your array	}}

So would that mean there is no way implemented in phaser?

Link to comment
Share on other sites

Hi,

 

If you go the CSV route it's pretty easy to get an array into a tilemap:

 

Looking through the Phaser 1.1.3 Code (as far as I know the latest stable, v1.1.3 - Built at: Fri Nov 29 2013 18:20:59 ) I found that the mapparser could also read CSV and has the option to take the csv Data directly in a string.

 

 

game.load.tilemap('level', null, generateLevel(), Phaser.Tilemap.CSV );

 

And generateLevel returns a CSV string looking like this:

 

"0,0,0,0,0\n1,1,0,0,1\n1,12,0,15,1\n"

 

(That would be a tilemap of 5x3 tiles)

 

Works like a charm. (Array to csv should be easy, just two nested for loops and sring concat)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...