Error using loadgraphic - Error #2015: Invalid BitmapData.



  • I was working on a game with an inventory system, which I created as a singleton and planned to add to any state that required it. Just before loading the first scene in the game, I call a method called setupInventory, which is supposed to initialize a sprite for the background of the inventory, and a group for the items that are placed in it.

    I call the method just before creating the PlayState, as shown here:

    class Game extends FlxGame
    {
      public static var instance:Game;
    	
         public function new() {
                 .......
         }
    		
         override public function update():Void {
         	super.update();
         }
    	
         public static function startGame():Void {
         	Inventory.instance.setupInventory();
     	changeScene(new PlayState());
         }
    	
         public static function changeScene(scene:FlxState):Void {
    	FlxG.switchState(scene);
         }	
    }
    

    and setupInventory currently simply does this:

        public function setupInventory():Void {
          backgroundImage = new FlxSprite(700, 64);
          backgroundImage.loadGraphic(AssetPaths.hammer__png);
    }
    

    but when I build and run this in flash, I get an error:

    ArgumentError: Error #2015: Invalid BitmapData.
    	at flash.display::BitmapData/copyPixels()
    	at flixel.graphics.frames::FlxFrame/paint()
    	at flixel::FlxSprite/updateFramePixels()
    	at flixel::FlxSprite/calcFrame()
    	at flixel::FlxSprite/draw()
    	at flixel.group::FlxTypedGroup/draw()
    	at flixel.group::FlxTypedGroup/draw()
    	at flixel::FlxState/draw()
    	at flixel::FlxGame/draw()
    	at flixel::FlxGame/onEnterFrame()
    

    and I really do not know what this means, or why I would get it. I use the same asset at another point and it does not seem to have any errors. When I test using neko I do not get an error (though I am having some problems getting everything to draw, but I think that is a post for another day).



  • I would not recommend writing a new class extending FlxGame but instead use signals in FlxG.signals like this:

    Probably somewhere in your Main.hx:

    // setup inventory before creating first state
    FlxG.signals.preStateCreate.addOnce(Inventory.instance.setupInventory);
    
    // setup inventory after creating first state
    FlxG.signals.gameStarted.add(Inventory.instance.setupInventory);
    


  • I set things up differently so that I am no longer extending FlxGame, but I am still getting the same error.



  • How different is the setup exactly?
    Maybe create a test project which only has the Inventory and load the background before first state is created just like what you're doing right now.


  • administrators

    Flixel's graphics cache is cleared before state switches, so the BitmapData of the sprite may already be disposed when you enter PlayState (since you created the sprite beforehand).



  • If what @Gama11 said is right, then try:

    sprite.graphic.persist = true;
    

Log in to reply