Jump to content

Tilemap Copy & Paste issues


Recommended Posts

I'm trying to do an endlessly scrolling tilemap. I found this thread where you said they weren't supported as-is. My thought is to take a tilemap which is twice as tall as needed and automatically double its tiles so I can simulate an endless map, jumping back to the beginning once I reach the end.


Looking through the docs, I thought this would work:

var newTiles = map.copy(0, 0, 20, 20);map.paste(0, 20, newTiles);

Here, map is a Tilemap which has 20 columns and 20 rows of content. I'm trying to paste those cells in, starting 20 rows down. The map itself is actually 20 columns x 40 rows.


I get the following console error:

Uncaught TypeError: Cannot set property '0' of undefined


I'm able to accomplish it this way instead:

map.forEach(function(tile) {    if(tile.hasOwnProperty('index')){        map.putTile(tile.index, tile.x, tile.y + 20);    }    }, 0, 0, 0, 20, 20);

But I'm curious why the first way doesn't work, or how I can fix it. Or if I'm perhaps going around all of this a very silly way.


The reason I want to duplicate these on-the-fly instead of just doing it in a Tile Editor is that the user will be modifying the map while the game is running.

Link to comment
Share on other sites

Thanks for the reply, I tried it just now with 1.1.4, and specified layer 0:

 var newTiles = map.copy(0, 0, 20, 20, 0); map.paste(0, 20, newTiles, 0);

And I get the same error as before: Cannot set property '0' of undefined.

I'm not sure how to know which layer this should happen on. I looked in the default args and a missing layer in the function call defaults to this.currentLayer, which is undefined in my case. Maybe I'm doing something else wrong?


I'll switch to non-minified Phaser and see if I can figure out what's going on underneath.

Link to comment
Share on other sites

  • 2 weeks later...

I have some questions regarding the tilemap copy and paste functions too. I'm now using Phaser 1.1.4. 


I see that Copy and Paste are used a lot internally to copy a block of data, change it and then paste it back to the same position. I'm trying to use it to copy a block of tiles and paste it to a different position.


Lines 921-2 of the Tilemap.js have:

var diffX = tileblock[1].x - x;
var diffY = tileblock[1].y - y;


But if I am pasting a block copied from {x:20, y:0} to {x:0, y:0} that gives a diffX of 20 and diffY of 0. Should the subtraction be the other way around, e.g. x-tileblock[1].x ? As, I'm pretty sure, currently the for loop will then start pasting the data at this.layers[layer].data[0][40].


Also, it seems that if tiles are left empty in Tiled then the data contains null values and this throws an error as an x and y value are assumed. I've got around this particular issue by having a blank tile and filling the map before I add any other tiles.


Am I just trying to use these functions incorrectly? For context, I'm trying to create an endless tilemap by copying sections of the map and pasting them to a blank area of the map that is shown on the screen.

Link to comment
Share on other sites

I would say for now try changing the code to be the other way around and test it. If it still works as expected then submit a pull request and I'll merge it in. Failing that if you raise an issue on github I'll go through it when I next update tilemaps (which will be quite soon, but not immediately).

Link to comment
Share on other sites


  • Recently Browsing   0 members

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