AS3 public variable won't store values - actionscript-3

first time poster here.
So I'm building a "multi-RSS feed reader" and I've broken this into 2 classes, a "TechFeed.as" and a "Feed.as"; The tech feed instantiates multiple feeds by passing URL's to a new Feed object.
My issue at the moment is that in the Feed.as I have a couple public variables which are used in TechFeed.as to display basic details about the feed, however, these variables refuse to retain any value I give them within Feed.as's functions, and are turning up "null".
Edit: Also maybe something to note; TechFeed.as is the main AS file the stage uses.
TechFeed.as
package {
import flash.display.MovieClip;
public class TechFeed extends MovieClip {
private var feed_one:Feed = new Feed("http://feeds.feedburner.com/crunchgear");
private var feed_two:Feed = new Feed("http://feeds.mashable.com/Mashable");
private var feed_three:Feed = new Feed("http://www.engadget.com/rss.xml");
public function TechFeed() {
trace(feed_one.feed_title);
//feed_name.text = feed_one.getFeedTitle();
}
}
}
Feed.as
package {
import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;
public class Feed {
//Public variables to display feed info
public var feed_title:XMLList;
public var feed_desc:String;
public var feed_link:String;
//Setting up variables which help load the feeds
private var feedXML:XML;
//Create a loader to load an external URL
private var loader:URLLoader = new URLLoader();
public function Feed(inURL:String = "") {
//Load the xml document
loader.load(new URLRequest(inURL));
loader.addEventListener(Event.COMPLETE,onLoaded);
}
//When the loader had loaded the xml document, pass that into a variable for use.
private function onLoaded(e:Event):void {
feedXML = new XML(e.target.data);
// break down the xml document elements into singular XML array lists
//Feed details
this.feed_title = feedXML.channel.title;
this.feed_link = feedXML.channel.link;
this.feed_desc = feedXML.channel.description;
trace(this.feed_title);
}
}
}
Any help will be greatly appreciated :)
Thanks,
Geoff

Your problem is timing you are trying to write the data from the feed before it is returned from the server.
In this example I assign a function to call back on when the data is loaded.
package {
import flash.display.MovieClip;
public class TechFeed extends MovieClip {
private var feed_one:Feed;
private var feed_two:Feed;
private var feed_three:Feed;
public function TechFeed() {
feed_one= new Feed("http://feeds.feedburner.com/crunchgear",assignResults);
feed_two= new Feed("http://feeds.mashable.com/Mashable",assignResults);
feed_three= new Feed("http://www.engadget.com/rss.xml",assignResults);
}
public function assignResults(value:value):void{
feed_name.text = value;
}
}
}
package {
import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;
public class Feed {
//Public variables to display feed info
public var feed_title:XMLList;
public var feed_desc:String;
public var feed_link:String;
//Setting up variables which help load the feeds
private var feedXML:XML;
//Create a loader to load an external URL
private var loader:URLLoader = new URLLoader();
private var _callBackFunc:Function;
public function Feed(inURL:String = "", callBackFunc:Function) {
this._callBackFunc = callBackFunc;
//Load the xml document
loader.load(new URLRequest(inURL));
loader.addEventListener(Event.COMPLETE,onLoaded);
}
//When the loader had loaded the xml document, pass that into a variable for use.
private function onLoaded(e:Event):void {
feedXML = new XML(e.target.data);
// break down the xml document elements into singular XML array lists
//Feed details
this.feed_title = feedXML.channel.title;
this.feed_link = feedXML.channel.link;
this.feed_desc = feedXML.channel.description;
this._callBackFunc(this.feed_title);
}
}
}

If the problem is that feed_one.feed_title is coming up null in the TechFeed() ctor, that is because you are not waiting for the feed to finish loading.
What you aught to do is dispatch an event from the Feed when it is done loading and processing the data, catch it in TechFeed and then use the public variables as you please (this also means that your Feed class will have to subclass EventDispatcher):
In Feed class:
private function onLoaded(e:Event):void {
feedXML = new XML(e.target.data);
// break down the xml document elements into singular XML array lists
//Feed details
this.feed_title = feedXML.channel.title;
this.feed_link = feedXML.channel.link;
this.feed_desc = feedXML.channel.description;
trace(this.feed_title);
dispatchEvent(new Event(Event.COMPLETE));////Dispatch Event
}
In TechFeed class:
public function TechFeed() {
trace(feed_one.feed_title);//This will trace "null"
feed_one.addEventListener(Event.COMPLETE, dataLoaded);//add event listener
}
private function dataLoaded(e:Event):void{
var feed = Feed(e.currentTarget);
feed.removeEventListener(Event.COMPLETE, dataLoaded);//remove event listener to prevent memory leaks
trace(feed.feed_title);// This will trace the correct title
}

Related

save and load using sharedObject in AS3

i am currently doing an experiment to save each name of the user(not replacing it every time we save the new one).
my problem is the getLocal() function.I defined a variable with the type of string to make the SharedObject store the data to local storage with the name of the variable i defined which it is the name of the each user.
but i get this error:
Error #2134: Cannot create SharedObject. at
flash.net::SharedObject$/getLocal() at classes::Main/saveNewUser()
at classes::Main/promtSave()
here is my code
package classes {
import flash.display.MovieClip;
import flash.net.SharedObject;
import flash.text.TextField;
import classes.user.User;
public class Main extends MovieClip {
private var _user:User;
private var _properties:Object;
private var validOrNot_txt:TextField;
private var _name_input_txt:TextField;
//define a shared object to store our user data
private var database:SharedObject;
//define an array that will hold all users
private var userList:Array;
private var nameEntered:String;
public function Main() {
userList = [];
_name_input_txt = Name_input;
validOrNot_txt = valid_or_not_TXT;
validOrNot_txt.autoSize = 'center';
//_properties={Name:String,age:int,gender:String};
// constructor code
}
public function get user():User{
return _user;
}
private function saveNewUser():void{
nameEntered=_name_input_txt.text;
_user = new User();
_user._Name = _name_input_txt.text;
database = SharedObject.getLocal(nameEntered);
userList.push(_user);
database.data.Name = _user._Name;
}
}
}
here's the code in the timeline
import flash.events.MouseEvent;
login_btn.addEventListener(MouseEvent.CLICK, promtSave);
login_btn.buttonMode=true;
trace(Name_input.length);
function promtSave(e:MouseEvent):void{
if(Name_input.length>1){
saveNewUser();
}else{
throw(new Error('you did not enter anything'));
}
}
anybody, please help me with this :'(

How to return a variable from an actionscript Event? Loading XML file

i'm trying to load an xml file from my assets folder.
I wrote this function :
public static function loadXML(i_fileURL:String):XML // i want to return the actual loaded xml here
{
var xml:XML;
var ldr:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(i_fileURL);
ldr.addEventListener(Event.COMPLETE, onXMLLoad);
ldr.load(request);
//how can i return the loaded xml?
}
public static function onXMLLoad(e:Event):void
{
var ldr:URLLoader = URLLoader(e.target);
var myxml:XML = new XML(ldr.data);
trace(myxml.toXMLString());
//how can i return myxml to loadXML function?
}
Is there a different way to do this?
Thank you!
You can do something like a promise or future, where you return empty XML and then populate it with the actual XML when the call returns. Since you are using Flex, you have access to data binding, which should allow this approach to work just fine.
Note that you really shouldn't be using static methods for this, and your onXMLLoad member has no reason to be exposed. Here's what the updated code might look like:
package service {
public class XMLLoader {
//note that the existence of this variable means that you need
//to create a new instance of the Class each time you make a call.
protected var future:XML;
protected var _url:String;
public function loadXML(url:String):XML {
_url = url;
var request:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.Complete, onLoad);
loader.addEventListener(IoErrorEvent.IO_Error, onFail);
loader.addEventListener(SecurityErrorEvent.Security_Error, onFail);
future = ;
return future;
}
protected function onLoad(e:Event):void {
var loader:URLLoader = e.currentTarget as URLLoader;
var data:XML = loader.data as XML;
if (data) {
//you lose your original root node, because you want data
//binding to fire on the future object you returned.
future.setChildren(data.children());
}
}
protected function onFail(e:Event):void {
//both Security and IOerrors have a text property, but they
//can't both be cast to the same thing.
trace('failed to load', _url, e[text]);
}
}
}
One thing to be aware of with this method is that you need to keep a reference to the instance around until the data has updated, or else it could be garbage collected before it populates the future. Because of that, you're probably better off following the conventional approach of having your instance dispatch a custom event that carries the data that it was retrieving. If you want an example of that, post back and I can provide you an example.
you can't. because xml load request is asynchronous. when you called loadXML, onXMLLoad not yet arrived. so such work impossible asynchronous return. surely you can waiting the function process while loop using, but this method not good. because to full use a cpu resource, overkill. you must next process in onXMLLoad function. It's the most appropriate. or xml variable declared as global, and using a ENTER_FRAME or TimerEvent as a way to continue to operate if the xml is not null.
Since the URLLoader is asynchronous, it's not safe to create a static loader function as it would be quiet easy to mix up returned data during multiple calls. Even if you attempted to accomplish what you want with the use of Event.OPEN and a vector of URLs to keep track of the which completed data should belong to each URL, asynchronousity works on a first-come, first-served basis so it wouldn't be possible to have persistent alignment of the file URL and the returned data.
I suggest that you create instances of an XMLLoader class that uses a custom XMLLoaderEvent, which will return both the xml data and the associated file URL. The following code is untested, but with possible typos aside, it should work as intended.
Use Case
var xmlLoader:XMLLoader = new XMLLoader();
xmlLoader.addEventListener(XMLLoaderEvent.COMPLETE, xmlLoadCompleteEventHandler);
xmlLoader.load("myXMLFile.xml");
function xmlLoadCompleteEventHandler(event:XMLLoaderEvent):void
{
xmlLoader.removeEventListener(XMLLoaderEvent.COMPLETE, xmlLoadCompleteEventHandler);
trace(event.type, event.fileURL, event.xml);
}
XMLLoader.as
package
{
//Imports
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
//Class
public class XMLLoader extends EventDispatcher
{
//Properties
private var loader:URLLoader;
private var fileURL:String;
//Constructor
public function XMLLoader():void
{
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loadCompleteEventHandler);
}
//Load
public function load(fileURL:String):void
{
this.fileURL = fileURL;
loader.load(new URLRequest(fileURL));
}
//Load Complete Event Hanlder
private function loadCompleteEventHandler(event:Event):void
{
loader.removeEventListener(Event.COMPLETE, loadCompleteEventHandler);
dispatchEvent(new XMLLoaderEvent(XMLLoaderEvent.COMPLETE, fileURL, XML(loader.data)));
}
}
}
XMLLoaderEvent.as
package
{
//Imports
import flash.events.Event;
//Class
public class XMLLoaderEvent extends Event
{
//Constants
public static const COMPLETE:String = "complete";
//Properties
public var xml:XML;
public var fileURL:String;
//Constructor
public function XMLLoaderEvent(type:String, fileURL:String = null, xml:XML = null)
{
super(type);
this.xml = xml;
this.fileURL = fileURL;
}
//Clone
public override function clone():Event
{
return new XMLLoaderEvent(type, fileURL, xml);
}
//To String
public override function toString():String
{
return formatToString("XMLLoaderEvent", "type", "fileURL", "xml");
}
}
}

Initialize Assets after Preload

Whenever I export the .swf file of my Flash game, I am receiving "TypeError: Error #1009: Cannot access a property or method of a null object reference.", along with a Runtime Shared Library Preloading Warning for my preloader. I have my timeline organized so that the first and third frames are both empty along with a stop(); command in the Actions layer. The second frame contains a single MovieClip that contains all of my exported assets, which are going to be initialized in the third frame of the timeline. None of my assets, except for the preloader, are exported in the first frame. What changes should I make to my Document Class for it to initialize the assets in the third frame?
Document Class:
package com.gameEngine.documentClass
{
import flash.events.*;
import flash.display.*;
import flash.geom.Point;
import com.gameEngine.assetHolders.*;
import com.gameEngine.assetHolders.Levels.*;
public class Document extends MovieClip
{
private static var _document:Document;
private var preloader:Preloader;
public var mcMain:Player;
public var restartButton:RestartButton;
public var spawnArea:SpawnArea;
public var level_1:Level_1;
public var level_2:Level_2;
public var level_3:Level_3;
public function Document()
{
addEventListener(Event.ADDED_TO_STAGE, init);
_document = this;
preloader = new Preloader(390, this.loaderInfo);
this.addChild(preloader);
preloader.addEventListener("loadComplete", loadAssets);
preloader.addEventListener("preloaderFinished", showLogo);
mcMain = new Player(this);
restartButton = new RestartButton(this);
spawnArea = new SpawnArea();
level_1 = new Level_1(this);
level_2 = new Level_2(this);
level_3 = new Level_3(this);
this.addChild(restartButton);
this.addChild(spawnArea);
this.preloader.x = 400;
this.preloader.y = 250;
restartButton.x = 822.95;
restartButton.y = 19;
spawnArea.x = 400;
spawnArea.y = 250;
trace ("Document Class Initialized");
// constructor code
}
public static function getInstance():Document
{
return _document;
}
private function loadAssets(event:Event):void
{
this.play();
}
private function showLogo(event:Event):void
{
this.removeChild(preloader);
}
public function init(event:Event)
{
if (stage.contains(spawnArea))
{
addChild(mcMain);
}
mcMain.x = spawnArea.x;
mcMain.y = spawnArea.y;
}
}
}
Preloader Class:
package com.gameEngine.assetHolders
{
import com.gameEngine.documentClass.*;
import flash.display.*;
import flash.events.*;
public class Preloader extends MovieClip
{
private var fullWidth:Number;
public var loaderInfo:LoaderInfo;
public function Preloader(fullWidth:Number = 0, loaderInfo:LoaderInfo = null)
{
this.fullWidth = fullWidth;
this.loaderInfo = loaderInfo;
addEventListener(Event.ENTER_FRAME, checkLoad);
}
private function checkLoad (event:Event):void
{
if (loaderInfo.bytesLoaded == loaderInfo.bytesTotal && loaderInfo.bytesTotal != 0)
{
dispatchEvent(new Event("loadComplete"));
phaseOut();
}
updateLoader(loaderInfo.bytesLoaded / loaderInfo.bytesTotal);
}
private function updateLoader(num:Number):void
{
progressBar.width = num * fullWidth;
}
private function phaseOut():void
{
removeEventListener(Event.ENTER_FRAME, checkLoad);
progressBar.gotoAndPlay(2);
if (progressBar.currentFrame == progressBar.totalFrames)
{
phaseComplete();
}
}
private function phaseComplete() : void
{
dispatchEvent(new Event("preloaderFinished"));
}
}
}
You have a lot of race conditions going on here. Many of these events could occur at relatively random times in relation to one another . . . you have to think asynchronously. That is, there can be no assumption that any object exists. E.g., in Document.init(), you check is if the spawnArea exists, but it is almost guaranteed not to at that point, and you never check for it again.
Without making any specific changes, I can recommend a generic solution. For any object (objB) you want loaded after another object (objA) is loaded, have objB created in the objA's ADDED_TO_STAGE handler. A simple example would be:
var objA:Whatever;
var objB:WhateverElse;
[...]
objA = new Whatever();
objA.addEventListener(Event.ADDED_TO_STAGE, objAAddedHnd);
[...]
public function objAAddedHnd(event:Event)
{
// remove the event, if no longer needed:
objA.removeEventListener(Event.ADDED_TO_STAGE, objAAddedHnd);
objB = new WhateverElse();
objB.addEventListener(Event.ADDED_TO_STAGE, objBAddedHnd);
}
[...]
public function objBAddedHnd(event:Event)
{
// remove the event, if no longer needed:
objB.removeEventListener(Event.ADDED_TO_STAGE, objBAddedHnd);
// and so on . . .
}
At this point, it shows that you would need to plan the timeline of object creation.

AS3: Structuring custom classes

I wrote a class ANDGatter.as which has 4 child objects "AndIONode0-3". See code. I got drag-drop functionality working but the order in which I add the eventlisteners seems to be wrong. Should I add listeners before or after the addChild methods ? I tried both ways, but when instanciating 2 ANDGatter in my Application I get a Null-pointer Exception during the first drag-drop action. How can I fix this ?
package classes{
import classes.ConnectionLine;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import mx.containers.Canvas;
import mx.controls.Alert;
import mx.core.DragSource;
import mx.events.DragEvent;
import mx.managers.DragManager;
public class AndGatter extends Canvas
{
public var AndIONode0:Canvas;
public var AndIONode1:Canvas;
public var AndIONode2:Canvas;
public var AndIONode3:Canvas;
public var nodeWidth:int=10;
public var nodeHeight:int=10;
public var nodeColor:uint=0x919191;
public var gatterColor:uint=0x474747;
public var startconnector:DisplayObject;
public var endconnector:DisplayObject;
public function AndGatter()
{
super();
super.width=50;
super.height=50;
this.setStyle("backgroundColor",gatterColor);
/*EingangsNode0 für AndGatter*/
AndIONode0=new Canvas();
AndIONode0.name="And Output Node";
AndIONode0.width=nodeWidth;
AndIONode0.height=nodeHeight;
AndIONode0.x=40;
AndIONode0.y=20;
AndIONode0.setStyle("backgroundColor",nodeColor);
addChild(AndIONode0);
AndIONode1=new Canvas();
AndIONode1.name="And Input Node 1";
AndIONode1.width=nodeWidth;
AndIONode1.height=nodeHeight;
AndIONode1.x=0;
AndIONode1.y=5;
AndIONode1.setStyle("backgroundColor",nodeColor);
addChild(AndIONode1);
AndIONode2=new Canvas();
AndIONode2.name="And Input Node 2";
AndIONode2.width=nodeWidth;
AndIONode2.height=nodeHeight;
AndIONode2.x=0;
AndIONode2.y=20;
AndIONode2.setStyle("backgroundColor",nodeColor);
addChild(AndIONode2);
AndIONode3=new Canvas();
AndIONode3.name="And Input Node 3";
AndIONode3.width=nodeWidth;
AndIONode3.height=nodeHeight;
AndIONode3.x=0;
AndIONode3.y=35;
AndIONode3.setStyle("backgroundColor",nodeColor);
addChild(AndIONode3);
AndIONode0.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
AndIONode1.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
AndIONode1.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
AndIONode2.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
AndIONode2.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
AndIONode3.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
AndIONode3.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
}
private function mouseMoveHandler(event:MouseEvent):void {
var dragInitiator:Canvas=Canvas(event.currentTarget);
startconnector=dragInitiator;
var ds:DragSource = new DragSource();
ds.addData(id, 'start');
DragManager.doDrag(dragInitiator, ds, event);
}
private function dragEnterHandler(event:DragEvent):void {
if (event.dragSource.hasFormat('start')) {
var dropTarget:Canvas=Canvas(event.currentTarget);
endconnector=dropTarget;
DragManager.acceptDragDrop(dropTarget);
}
}
private function dragDropHandler(event:DragEvent):void {
/*var connector:ConnectionLine = new ConnectionLine(startconnector,endconnector,true);
stage.rawChildren.addChild(connector);*/
Alert.show("Verbindung hergestellt zwischen \n"+startconnector.name+" und "+endconnector.name);
}
}
}
In dragDropHandler(), you're referencing startconnector.name. startconnector isn't assigned until mouseMoveHandler() is called, which only happens when you move your mouse over AndIONode0. You may need to adjust which AndIONodes you are adding your move listeners to in order to make sure that startconnector always has a value.
The following statement is failing
if (event.dragSource.hasFormat('start')) {
causing endconnector
endconnector=dropTarget;
to never be dropTarget which will basically be a null object.
Null objects will not have attributes.
So
Alert.show("Verbindung hergestellt zwischen \n"+startconnector.name+" und "+endconnector.name);
Will error out because of the request of an attribute on a null object
endconnector.name

Run-time loading of external assets and re-using preloaded assets in actionscript 3?

I'm creating a 2d flash game (coded in flex/actionscript 3) where assets are downloaded when they are needed. Currently I have it setup like this:
AssetLoader.as
package
{
import flash.display.Loader;
import flash.net.URLRequest;
public class AssetLoader extends Loader
{
//set vars
private var url:String = "http://test.com/client/assets/";
public function AssetLoader(url:String)
{
Logger.log("AssetLoader request: " + this.url + url);
var request:URLRequest = new URLRequest(this.url + url);
this.load(request);
}
}
}
Then, where I want to load the asset I do the following:
var asset:AssetLoader = new AssetLoader("ships/" + graphicId + ".gif");
asset.contentLoaderInfo.addEventListener(Event.COMPLETE, onShipAssetComplete, false, 0, true);
private function onShipAssetComplete(event:Event):void
{
var loader:Loader = Loader(event.target.loader);
shipImage = Bitmap(loader.content);
shipImage.smoothing = true;
addChild(shipImage);
}
The thing is, that this method doesn't check for already downloaded assets, so it will redownload them the second time the same asset is being requested (I think).
So, what I need is an array where all downloaded assets are stored, and on request the name of this asset is checked for existance in the array. So if it has already been downloaded, that asset from memory must be returned rather than redownloaded.
I could make the assetloader a static class, but I have to wait for the event to fire when it's done downloading the image - so I can't simply let a static function return the corresponding image. Any idea how I should do this?
EDIT for an attempt after comments:
package
{
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
public final class AssetManager
{
private static var assets:Object = {};
private static var preUrl:String = Settings.ASSETS_PRE_URL;
public static function load(postUrl:String):*
{
if (assets[postUrl])
{ //when the asset already exists
//continue
}
else
{ //the asset still has to be downloaded
var request:URLRequest = new URLRequest(preUrl + postUrl);
var loader:Loader = new Loader();
loader.load(request);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
function(event:Event):void
{
var loader:Loader = Loader(event.target.loader);
assets[postUrl] = loader.content;
}, false, 0, true);
}
}
}
}
EDIT2: another attempt
package
{
import flash.display.Loader;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLRequest;
public final class AssetManager
{
private static var assets:Object = {};
private static var preUrl:String = Settings.ASSETS_PRE_URL;
public static function load(postUrl:String):*
{
if (assets[postUrl])
{ //the asset already exists
var dispatcher:EventDispatcher = new EventDispatcher();
dispatcher.dispatchEvent(new CustomEvent(CustomEvent.LOAD_COMPLETE, assets[postUrl]));
}
else
{ //the asset still has to be downloaded
var request:URLRequest = new URLRequest(preUrl + postUrl);
var loader:Loader = new Loader();
loader.load(request);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
function(event:Event):void
{
var loader:Loader = Loader(event.target.loader);
assets[postUrl] = loader.content;
var dispatcher:EventDispatcher = new EventDispatcher();
dispatcher.dispatchEvent(new CustomEvent(CustomEvent.LOAD_COMPLETE, assets[postUrl]));
}, false, 0, true);
}
}
}
}
Then, I try the following:
var asset:AssetManager = AssetManager.load("ships/" + graphicId + ".gif");
asset.addEventListener(CustomEvent.LOAD_COMPLETE, onShipAssetComplete, false, 0, true);
But get an error, "undefined method addEventListener by a reference of the type static AssetManager" (roughly translated).
You could add a static object (used as a dictionary with urls for assets as keys and the content for assets as values) in the AssetLoader class and in the same time keep using the class in the way you're using it right now.
private static var assets:Object = {};
The difference would be that your class would need to check against that static object if the URL for the content has already been requested previously. If it has, dispatch the complete event immediately. If it hasn't, follow the normal routine and don't forget to populate your static object with the newly loaded asset.
Update:
This is a quick example of what I meant. I haven't had time to test this, but it should work.
Note:
You must invoke the loadAsset() method of the AssetLoader instances you create in order to actually load the asset. This is consistent with the way the Loader class we're extending works.
You should always add all event listeners BEFORE invoking the loadAsset() method. In your question you're calling the load() method from within the constructor and only afterwards add the event listener for Event.COMPLETE. This could produce strange results.
Here's the code:
package
{
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
public class AssetLoader extends Loader
{
private static const BASE_URL:String = 'http://test.com/client/assets/';
public static var storedAssets:Object = {};
private var assetURL:String;
private var urlRequest:URLRequest;
private var cached:Boolean = false;
public function AssetLoader(url:String):void
{
trace('Loading: ' + url);
assetURL = url;
if (storedAssets[assetURL] != null)
{
cached = true;
trace('Cached');
}
else
{
trace('Loading uncached asset');
urlRequest = new URLRequest(BASE_URL + assetURL);
contentLoaderInfo.addEventListener(Event.COMPLETE, OnAssetLoadComplete);
}
}
public function loadAsset():void
{
if (cached)
loadBytes(storedAssets[assetURL]);
else
load(urlRequest);
}
private function OnAssetLoadComplete(event:Event):void
{
storedAssets[assetURL] = contentLoaderInfo.bytes;
trace('Loaded ' + contentLoaderInfo.bytesLoaded + ' bytes');
}
}
}
Update 2:
Here's how one would use the class above:
var assetLdr:AssetLoader = new AssetLoader("ships/" + graphicId + ".gif");
assetLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onShipAssetComplete);
assetLdr.loadAsset();
private function onShipAssetComplete(event:Event):void
{
var shipImage:Bitmap = Bitmap(event.target.loader.content);
// Do stuff with shipImage
}
Perhaps you should take a look at Bulk Loader. It does the kinds of things your looking to do. If you really want to use a custom solution, it would be a great point of reference, but why reinvent the wheel?
Tyler.
Here is an alteration of your load command to capture the resourceId
public function load(postUrl:String):*
{
var index:int;
if ((index = assetExists(postUrl)) != -1)
{
dispatchEvent(new CustomEvent(CustomEvent.LOAD_COMPLETE, asset[postUrl]));
}
else
{
//the asset still has to be downloaded
var request:URLRequest = new URLRequest(preUrl + postUrl);
var loader:Loader = new Loader();
loader.load(request);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
function(event:Event)
{
// NOTE: not sure on scoping here ...
// pretty sure you're this will be the Loader
// if not just read it off the event like you were before
assets[postUrl] = content;
dispatchEvent(new CustomEvent(CustomEvent.LOAD_COMPLETE, asset[postUrl]));
}, false, 0, true);
}
}
/* In a new file */
public class CustomEvent extends Event
{
public static const LOAD_COMPLETE:String = "CustomEvent_LoadComplete";
// If you know the type you should use it (e.g. Sprite/DisplayObject)
public var content:*;
public function CustomEvent(type:String, _content:*)
{
content = _content;
super(type);
}
}
Note: when you write an Event descendant you should also override the toString and clone methods. I've also cheated on the constructor since you may want to pass through weakReferences and things like that.