AS3 new BitmapData(w,h); gets ArgumentError: Error #2015: Invalid, works for a while then stops with this error

StackOverflow https://stackoverflow.com/questions/11404908

  •  19-06-2021
  •  | 
  •  

Question

I have a game that produces new chunks of terrain as the player explores, to allow for a seemingly unlimited level size. It's been working perfectly, but I decided to let my character travel in one direction for a long time, and eventually it gets this error:

ArgumentError: Error #2015: Invalid BitmapData.
        at flash.display::BitmapData/ctor()
        at flash.display::BitmapData()

At this line of code:

this.new_chunk.background_bitmap_data = new BitmapData(this.level_obj.block_size *  this.new_chunk.blocks_wide + this.level_obj.oversize_bmd_offset,this.level_obj.block_size * this.new_chunk.blocks_tall + this.level_obj.oversize_bmd_offset,true,0x00000000);

I ran a trace(); on the variables/equations that provide the width and height values being used to create the bitmap data, and it outputs the same values constantly, as intended:

2150 1300

I ran my character in a number of different directions, and the same thing eventually happens regardless of direction. I know that the BitmapData isn't getting too large, because it's always 2150x1300.

Note: The error does not occur while staying in a small area, no matter how long I leave the game running, the error only occurs after traveling in one direction for a long time, generating numerous new chunks of level.

Any ideas why flash is throwing this error?

EDIT:

Here is a live demo of the game so you can get a better understanding of what's going on: http://test.webskethio.com/boxy/

(Controls are W.A.S.D for movement, hold shift to move faster.)

Each "chunk" is the size of the game screen itself, each chunk stores a cached BitmapData of all of the smaller BitmapDatas that make it up (the tiles of grass, trees, rocks, flowers, etc.) The chunk BitmapData is loaded to a Bitmap on the stage when the chunk is close enough to the stage that it can end up needing to be rendered.

EDIT 2:

I've uploaded a picture to illustrate what I believe is Amy's proposed method of rendering for my game that doesn't require the storage of many BitmapData objects:

enter image description here

The four corner colored areas are BitmapData objects that will be drawn to the one Bitmap on the screen (same size as the user's screen, represented in green.) The black area around the whole thing represents what at one point could have been other BitmapData objects which have been set to null since they are no longer needed.

The 4 corner BitmapData objects will be redrawn to the main Bitmap with new point coordinates as the player moves.

Was it helpful?

Solution

I was unable to reproduce your error.

I was running straight north with shift for around 5 minutes, memory consumption grown to 3+GiB and then I got out of RAM so it started to use swap and got very slow.

So, I guess you just run out of memory, and the error of flash is a bit weird for that case. Try freeing old bitmaps, or maybe even better reusing it. The recommended approach is usually having 4 bitmaps of the size of the screen at all times, reusing west ones for new regions at east when you go east, vice verse and same for north and south.

Also, I see a very annoying blinking white stripe. I dont know if that's actually the reason, but it usually happens when someone don't use double-buffering.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top