Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.18 / 5.00 3,534 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.80 / 5.00 4,200 ViewsI'm trying to write a very simple code using 2 classes. One class simply loads and XML the other parses the the loaded XML and find the string for what image I need to load. I may be going about this wrong and I know my issue is that I'm calling the image load before the xml is loaded. Any insight would really help here, I'm still very new to OOP and I'm trying to avoid doing this purely on the timeline.
Timeline code:
import XMLLoader;
import AdContainer;
var myGame:String = "Banner2";
var fileName:String = "loading_ads.xml";
var newXML:XMLLoader = new XMLLoader(myGame, fileName);
addChild(newXML);
var ad1Container:AdContainer = new AdContainer(newXML.myXML[myGame].ad1);
var ad2Container:AdContainer = new AdContainer(newXML.myXML[myGame].ad2);
ad1_mc.addChild(ad1Container);
ad2_mc.addChild(ad2Container);
AdContainer Class:
package {
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class AdContainer extends MovieClip {
private var _url:String = "";
private var _bytesLoadProgress:int = 0;
private var _loaded:Bitmap;
public function AdContainer(url:String) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressLoaded);
loader.load(new URLRequest(url));
}
public function onComplete(e:Event):void {
_loaded = new Bitmap(e.target.loader.contentLoaderInfo.content.bitmapData.clone());
this.addChild(_loaded);
}
public function progressLoaded(e:ProgressEvent):void {
_bytesLoadProgress = e.bytesLoaded / e.bytesTotal;
}
public function getProsess():int {
return _bytesLoadProgress;
}
}
}
XMLLoader class:
package {
import flash.events.*;
import flash.net.*;
import flash.xml.*;
import flash.display.Sprite;
public class XMLLoader extends Sprite{
public var myXML:XML;
public var waffles:String = 'meh';
private var loader:URLLoader = new URLLoader();
private var newFile:String;
private var newGame:String;
public function XMLLoader(myGame:String, fileName:String) {
newFile = fileName;
newGame = myGame;
parser();
}
private function parser():void {
// A listener to react after the xml has been loaded
loader.addEventListener(Event.COMPLETE, onLoaded);
//loading in our xml
loader.load(new URLRequest(newFile));
}
// Our reaction function
private function onLoaded(e:Event):void {
// Takes the xml data we loaded and parses its data
myXML = new XML(e.target.data);
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<Ads>
<Banner1>
<ad1>WH_ad1.png</ad1>
<ad2>WH_ad2.png</ad2>
</Banner1>
<Banner2>
<ad1>BV_ad1.png</ad1>
<ad2>BV_ad2.png</ad2>
</Banner2>
</Ads> Add an eventlistener to the XMLLoader (terrible name for a sprite btw) and wrap the code about the 'Adcontainers' in the event handler. Then dispatch the correct event in your'onLoaded' handler in the XMLLoader class.
You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.
At 11/8/12 09:49 PM, ProfessorFlash wrote: Add an eventlistener to the XMLLoader (terrible name for a sprite btw) and wrap the code about the 'Adcontainers' in the event handler. Then dispatch the correct event in your'onLoaded' handler in the XMLLoader class.
I'll try that (and yes naming wise my methods leave something to be desired, still learning)