Je pense que j'ai un probleme d'optimisation dans ma facon de charger les assets graphiques.
Voici la classe qui me sert a charger un asset graphique :
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.system.ImageDecodingPolicy;
import flash.system.LoaderContext;
import flash.utils.getTimer;
import events.Event_Context;
import starling.events.EventDispatcher;
import starling.textures.Texture;
public class Atlas_Loader extends EventDispatcher
{
public var path :String;
public var loader :Loader
public var sheet :BitmapData;
private var callback :Function;
private var loaderContext :LoaderContext
public function Atlas_Loader()
{
loaderContext = new LoaderContext();
loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD;
loader = new Loader ();
}
public function load (path:String, callback:Function):void
{
this.callback = callback;
loader.load(new URLRequest(path), null);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
}
private function onLoaded (e:Event):void
{
e.stopPropagation();
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
sheet = new BitmapData (loader.width, loader.height, true, 0x000000);
//sheet.copyPixels(loader.content.bitmapData, new Rectangle(0, 0, loader.width, loader.height), new Point (0,0));
sheet.draw(loader);
sheet.lock();
loader.unload();
loader = null;
if (callback != null)
{
callback();
}
else
{
dispatchEvent(new Event_Context(Event_Context.LOADING_COMPLETE, true));
}
}
private function onLoadError (e:IOErrorEvent):void
{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
trace ('LOADER ERROR : ' + e);
}
public function getTexture ():Texture
{
return Texture.fromBitmapData(sheet, false, true);
}
public function disposeView ():void
{
if(sheet != null)
{
sheet.dispose();
sheet = null;
}
}
}
Comme vous pouvez le voir dans la fonction onLoaded, je transform le contenu de mon loader en Bitmap data par le biais de .draw()
Mais mon petit doig me dit que ca serait plus rapide et plus optimise d'utiliser un simple copyPixel, ou encore d'acceder au bitmapData directement depuis le loader, ce qui, dans mon esprit, devrait etre possible, puisque on peut afficher un loader directement a l'ecran, c'est bien qu'il a un BitmapData cache quelquepart !
Mais bon voila, j'ai beau tourner et retourner la chose je ne trouve pas comment faire cela.
J'ai bien essaye de faire quelquechose comme ceci :
var bitmap:Bitmap = new Bitmap ();
bitmap = Bitmap(loader.content);
sheet.copyPixels(bitmap.bitmapData, new Rectangle(0, 0, bitmap.width, bitmap.height), new Point (0,0));
Seulement des fois ca marche et des fois pas ... Apparement certains assets sont consideres comme etant des MovieClip ce qui me cree des erreurs de conversion...
Bref, je m'en remet a votre infinie sagess, auriez vous quelques precieux conseils pour moi ?
Merci !