AS3 Loader ignoring .png transparency
-
12-09-2019 - |
Question
In Flash CS4, open a new document, change the background colour to something recognizeable (like magenta) and add the following code:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event){addChild(e.target.content);});
loader.load(new URLRequest('newsnakelub8.png'));
... replacing the .png filename with any you happen to have handy, I grabbed this one. Compile the .swf, and join me in a sigh of disapointment - the normally transparent .png now has a white background, not allowing the flash background colour to show through.
Is there a magical 'treatAsPNG24' property that I'm missing somewhere?
Solution
The image doesn't actually have a transparent background...
(source: liranuna.com)
For your pleasure, fixed image:
OTHER TIPS
The problem is your png. Try opening it in Photoshop and it won't be transparent there either. Try a different one - google it, or publish one from Flash or PS - and it will work fine. (That is, there's no setting to invoke, you just need a proper image.)
public static function getSource(stringURL:String):void {
currentProcessedURL = stringURL;
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest(stringURL);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
}
private static function onComplete(e:Event):void {
try{
var bitMap:BitmapData = new BitmapData(
(e.currentTarget as LoaderInfo).loader.width,
(e.currentTarget as LoaderInfo).loader.height);
bitMap.draw((e.currentTarget as LoaderInfo).loader);
var icon:BitmapSymbol = new BitmapSymbol();
icon.source = bitMap;
standardStringToImage[currentProcessedURL] = icon;
getSource(ALL_ARRAY.pop());
} catch(e:Error) {
trace(e);
}
}
Doing something like this will load Images with white backgrounds. The parameters , true, 0x00ffffff needs to be added to the BitmapData constructor. I found this thread because I was having white backgrounds loaded with my PNGs, and I knew they had transparent backgrounds. So, hopefully this will help anyone that had the same issue I did.
Since it's better to teach you how to fish...
If you're using GIMP 2 (free as in beer), just open the image, select Layer | Transparency... | Color to Alpha and pick the color you want to be converted to the transparent background.