View Full Version : Flash 9 - Loader class
hi all.
Flash 9 Preview is out, good, why not to try it? And where to start from? For me from the irreplaceable MovieClipLoader now Loader. Everything is good with the minimun code required:
Fla:
var url:String = "Qubo.jpg";
var urlReq:URLRequest = new URLRequest(url);
var l:Loader = new Loader();
l.load(urlReq);
addChild(l);
But when I try to transform those lines of code in a class nothing happens. What I miss?
Fla:
var url:String = "Qubo.jpg";
var sl:SimpleLoader = new SimpleLoader(url);
Class:
package {
import flash.display.*;
import flash.net.URLRequest;
public class SimpleLoader extends MovieClip {
public function SimpleLoader(url:String) {
var l:Loader = new Loader();
var urlReq:URLRequest = new URLRequest(url);
l.load(urlReq);
addChild(l);
}
}
}
Thanks.
Fall_X
07-06-2006, 09:38 AM
I'm guessing you forgot to add your SimpleLoader instance (sl) to something with addChild?
addChild(l); within SimpleLoader will add the loader to the SimpleLoader instance, but if the SimpleLoader isn't added to something, you won't see anything (it will exist, but it won't be visible).
Just a thought, if the SimpleLoader is just a container for an external swf/jpg/whatever, you should make it a Sprite and not a MovieClip.
Even better, make it extend Loader, and use this.load(urlReq), and drop the addChild on the next line - it would mean one less displayobject on stage.
it makes sense, for the first part it's true and working now:
fla:
var url:String = "Qubo.jpg";
var sl:SimpleLoader = new SimpleLoader(url);
addChild(sl);
When yout say "Even better...." i can't follow you, mind to show me with code what do u mean?
Thank you very much.
Fall_X
07-06-2006, 10:12 AM
Not tested, just modified your code a bit to give you an idea :
var url:String = "Qubo.jpg";
var sl:SimpleLoader = new SimpleLoader(url);
this.addChild(sl);
package {
import flash.display.*;
import flash.net.URLRequest;
public class SimpleLoader extends Loader {
public function SimpleLoader(url:String) {
var urlReq:URLRequest = new URLRequest(url);
this.load(urlReq);
// if you want events, you'll have to attach them to this.contentLoaderInfo
}
}
}
Now, the benefit compared to your code, is that you were creating a new movieclip (or sprite), and adding the loader to that, which is essentially a displayobject as well.
In my code, you have the same functionality as you have, but the class isn't just a new container for the loader, it's a modified loader.
now I understand and I want to go a bit forward. Thanks, your help, it's precious.
Fla:
var url:String = "Qubo.jpg";
var sl:SimpleLoaderLoadExtendLoader = new SimpleLoaderLoadExtendLoader();
sl.loadRequest(url);
addChild(sl);
class:
package {
import flash.display.*;
import flash.net.URLRequest;
public class SimpleLoaderLoadExtendLoader extends Loader {
public function SimpleLoaderLoadExtendLoader() {
super();
}
public function loadRequest(url:String):void {
var urlReq:URLRequest = new URLRequest(url);
load(urlReq);
}
}
}
Fall_X
07-06-2006, 12:30 PM
Not sure what this is all about :
public function SimpleLoaderLoadExtendLoader() {
super();
}
I'm pretty sure the constructor of the superclass was automatically called before the constructor of the inheriting class... Could you explain to me why you do that? (I'm pretty new to AS3 too).
this is my first attempt with As3.0 and it's true: super() is unnecessary. Now I've added an handler who manages all the events expected.
class:
package {
import flash.display.*;
import flash.net.URLRequest;
import flash.events.*
public class SimpleLoaderLoadExtendLoaderEvent extends Loader {
public function SimpleLoaderLoadExtendLoaderEvent() {
}
public function loadRequest(url:String):void {
var urlReq:URLRequest = new URLRequest(url);
load(urlReq);
handleEvents(this.contentLoaderInfo);
}
private function handleEvents(d:IEventDispatcher):void {
d.addEventListener(Event.COMPLETE, completeHandler);
d.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
d.addEventListener(Event.INIT, initHandler);
d.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
d.addEventListener(Event.OPEN, openHandler);
d.addEventListener(ProgressEvent.PROGRESS, progressHandler);
d.addEventListener(Event.UNLOAD, unLoadHandler);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function initHandler(event:Event):void {
trace("initHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function unLoadHandler(event:Event):void {
trace("unLoadHandler: " + event);
}
}
}
Fall_X
07-06-2006, 01:50 PM
I made a package (two classes) that can load a number of different clips, and it tells you when it's done loading, and you can also check the total progress. It's very rough, and needs a proper event model, but it's useable.
If you're interested in having a look, it can be found in the as3 scrolling engine I'm working on. Both can be downloaded here : http://gamesquid.com/TileSQUID/TileSQUID.zip
(this is my first as3 project, took me a little over a day, but I'm still adding new features, and I think it's quite good)
downloaded the zip file, I'll take a look.
Thanks, Ciao.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.