updateFramePixels() returns null



  • So my end goal is to take a sprite group and flatten it into a single sprite. Now to go about this my plan has become:

    1. Iterate over sprite group.
    2. Run updateFramePixels() to get the BitmapData
    3. Merge transparent BitmapData onto a common object
    4. Create an FlxGraphic from the BitmapData.

    Now the weirdness comes in that the original sprite group is not currently being rendered on the screen. We're in a substate, and the original sprite group is in a state that's not being drawn or updated. I suspect that because of that even forcing a drawFrame isn't suffecient to get updateFramePixels to return bitmap data.

    Now I would like to make this a generic static method, so adding the sprite group to the current scene and then deleting it seems a bit distasteful. So I was wondering if there's either A) A way I can get updateFramePixels() to return the BitmapData. B) A different way I can pull an FlxSprite's BitmapData. C) A different approach I can take to "flattening" an FlxSpriteGroup?



  • So I basically resolved this issue myself by working within FlxSprite and not trying to dick around with BitmapData. It also dawned on my that one of the objects I was trying to get the BitmapData of was an FlxSpriteGroup which is why it was coming up null but not throwing a type error. Anyways, this is my FlxGroup flattening solution in case anyone wants it.

    public static function flattenSpriteGroup(source:FlxSpriteGroup):FlxSprite {
    	/*
    	 * Flattening function, useful for when you're trying to construct 
    	 * an asset from multiple different assests, such as a multi-layered tile
    	 * or a character sprite.
    	 * 
    	 * Only works for sprites of the same size at (0,0). In theory should have worked
    	 * for any sprite sizes, but there's some weirdness with sprite positioning that
    	 * I can't be bothered with figuring out. 
    	 */
    	var finalSprite:FlxSprite = new FlxSprite();
    	source.forEach(function(sprite:Dynamic){
    		var workingSprite:FlxSprite = sprite;
    		if (sprite.graphic == null){
    			//Check if sprite is a group
    			if (sprite.group != null && sprite.group.length > 0){
    				Log.trace("recursion");
    				workingSprite = flattenSpriteGroup(sprite);
    			}
    			else {
    				//If no group is found then sprite just has no graphic so we ignore it
    				return;
    			}
    		}
    		if (finalSprite.graphic == null){
    			finalSprite.loadGraphic(workingSprite.graphic);
    		}
    		if (workingSprite != null){
    			//finalSprite.stamp(workingSprite, cast(workingSprite.x, Int), cast(workingSprite.y, Int));
    			finalSprite.stamp(workingSprite, 0, 0);
    		}
    	});
    	return finalSprite;
    }

Log in to reply