Jump to content

Creating a BitmapData Crashes Game


coltonoscopy
 Share

Recommended Posts

Hey guys,

 

So, I'm trying to generate a BitmapData from a TileMap in real time (basically, a 1:1 pixel for tile image to act as a minimap) and assign it to a Sprite, but the weight in computation from doing so on a map that's 2000x2000 tiles in size causes the entire game to crash. Here's my code:

 

// create new BitmapData the size of our tile map    bmd = game.add.bitmapData(map.width, map.height);    // for each tile in our map, set the pixel of our bmd to the right color    for (var i = 0; i < map.width * map.height; i++)    {        var x = i % map.height;        var y = Math.floor(i / map.height);        console.log("X: " + x + ", Y: " + y);        if (map.getTile(x, y, layer).index == tileTypes.grass)            bmd.setPixel(x, y, 0x00, 0xFF, 0xFF, 0xFF);        if (map.getTile(x, y, layer).index == tileTypes.ocean)            bmd.setPixel(x, y, 0xFF, 0x00, 0xFF, 0xFF);    }    var bmdSprite = game.add.sprite(1000, 510, bmd);    bmdSprite.fixedToCamera = true;    bmdSprite.bringToTop();

What would the best solution to this be? Am I perhaps doing something fundamentally wrong? Thanks for your help!

 

Best,

Colton

Link to comment
Share on other sites

I think the only reason it's crashing is because of the setPixel 'immediate' parameter. This defaults to `true`. For something this intense I would set it to `false` and then do: bmd.update after the loop has finished.

 

 

Even so, yes, 2000x2000 is a large bitmap to hold in memory and will crash old devices. Why not just render a portion of it? You surely can't be displaying the whole map at once anyway.

Link to comment
Share on other sites

Hi Rich,

 

Thanks for your reply! I tried setting the immediate flag to false in the setPixel calls and then calling update() once the loop has finished, but unfortunately, the page still crashes :/ What I wanted to do originally was just render the TileMapLayer to a new Sprite and then render a scaled version of that, which seems like it would be more efficient, and as linked in the following thread, but nobody responded:

http://www.html5gamedevs.com/topic/12664-creating-rendertexture-from-tilemaplayer/

The goal is simply to take the current TileMapLayer being rendered and render a small (200x140 or so) version of the entire thing so the player can see what the entire map looks like at a glance. If you've played Final Fantasy 6, it's sort of what happens in that game during the overworld, except that map is likely pre-constructed.

 

Best,

Colton

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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