A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: XML.onLoad = function using OOP

  1. #1
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357

    XML.onLoad = function using OOP

    Hi guys,

    I've been playing with XML for a while to load dynamic maps into a game i've been working on and was wondering if anyone knew why this shouldn't work...

    Code:
    map = function () {
    	// Load XML from the relevant URL
    	this.mapXML = new XML();
    	this.mapXML.load("mapXML_1.xml");
    	this.mapXML.onLoad = this.traceXML;
    };
    map.prototype.traceXML = function() {
    	trace(this.mapXML);
    };
    Map = new map();
    I want to keep mapXML within the map class so I can call it using this.mapXML within the map methods!

    Thanks

    RipX

  2. #2
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    nor does it like it called as a method of mapXML

    Code:
    mapXML = new XML();
    mapXML.load("mapXML_1.xml");
    mapXML.prototype.traceXML = function() {
    	trace(this.mapXML);
    };
    mapXML.onLoad = this.traceXML;
    It seems as if onLoad can't see the class to call the method :\ or is it me?

    RipX

  3. #3
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    No, it's not you. You have a scope problem. You can solve this by two different ways:
    Code:
    map = function () {
    	// Load XML from the relevant URL
    	this.mapXML = new XML();
    	this.mapXML.load("mapXML_1.xml");
    	this.mapXML.mapInstance = this;
    	this.mapXML.onLoad = this.traceXML;
    };
    map.prototype.traceXML = function(success) {
    	if (success) {
    		trace(this.mapInstance.mapXML);
    	}
    };
    Map = new map();
    ... or the more "correct" way of doing it, if you're writing AS 2.0/3.0 (since then it's not valid to add a property called "mapInstance" to the XML object):
    Code:
    import mx.utils.Delegate;
    
    map = function () {
    	// Load XML from the relevant URL
    	this.mapXML = new XML();
    	this.mapXML.load("mapXML_1.xml");
                // Runs traceXML in the scope of this (map) when onLoad is triggered
    	this.mapXML.onLoad = Delegate.create(this, this.traceXML);
    };
    map.prototype.traceXML = function(success) {
    	if (success) {
    		trace(this.mapXML);
    	}
    };
    Map = new map();

  4. #4
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    thanks strille, thought i was going crazy for a minute Well..3 hours!

    RipX

  5. #5
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Just a quick word of warning, because it looks like you're using AS1.0 (If you're not, and it's lazy AS2 you might get away with it, not sure) but this type of code caused me many headaches:

    Code:
    Map = new map()
    If you ever need to refresh Map, then the map object is gone, because of case-insensitivity. Think of the children and do not name instances the same as 'classes'. (Just learning AS3, and thankfully it's all case-sensitive now.)
    jonmack
    flash racer blog - advanced arcade racer development blog

  6. #6
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Yeah, sorry twas a bad example of coding since I don't use classes often, i'm using AS1 but have a new issue:
    Code:
    //////////////////////////////////////////////////////////////////////
    // mapClass constructor function
    //////////////////////////////////////////////////////////////////////
    mapClass = function () {
    };
    //////////////////////////////////////////////////////////////////////
    // XML load method
    //////////////////////////////////////////////////////////////////////
    mapClass.prototype.loadXML = function() {
    	this.mapXML = new XML();
    	this.mapXML.load("mapXML_1.xml");
    	this.mapXML.ignoreWhite = true;
    	this.mapXML.mapData = this;
    	this.mapXML.onLoad = this.traceXML;
    };
    //////////////////////////////////////////////////////////////////////
    // XML loading result method
    //////////////////////////////////////////////////////////////////////
    mapClass.prototype.traceXML = function(success) {
    	if (success) {
    		trace(this.mapData.mapXML);
    		this.parseXML();
    	} else {
    		trace("XML connection error");
    	}
    };
    //////////////////////////////////////////////////////////////////////
    // XML parce method
    //////////////////////////////////////////////////////////////////////
    mapClass.prototype.parseXML = function() {
    	trace("parse you annoying git");
    };
    //////////////////////////////////////////////////////////////////////
    // main
    //////////////////////////////////////////////////////////////////////
    mapClassInstance = new mapClass();
    mapClassInstance.loadXML();
    For some reason, this won't call the parseXML method from the traceXML :| Any ideas chaps?

    RipX
    Last edited by RipX; 11-04-2006 at 10:15 AM.

  7. #7
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666
    also,and this is no major bug,just not the ideal way to do it:
    code:

    this.mapXML.load("mapXML_1.xml");
    this.mapXML.onLoad = this.traceXML;



    You should always define the onLoad handler before you trigger the load call.
    So it should be the other way round,like this:
    code:

    this.mapXML.onLoad = this.traceXML;
    this.mapXML.load("mapXML_1.xml");



    Just nitpicking but yeah,as you asked how to improve it..
    And funny,it almost feels retro nostalgia to me now seeing such manner xml handling now that i´ve worked some time with AS3 (As there you use the loader class for all kinds of loading,handle xml different and do classes different,too of course ). Just wondered myself how quickly one gets used to the new and then looking at the old already feels weird somehow

  8. #8
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    cheers tom, amended that but the issue I have is still unresolved! Any advice on the method call?? Anyone??

    RipX

  9. #9
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666
    oh,i didn´t try the code,thought your actual problem was already solved
    So to go with an AS1 type solution similar to yours like this:
    code:

    //////////////////////////////////////////////////////////////////////
    // MapHandler constructor function
    //////////////////////////////////////////////////////////////////////
    MapHandler = function () {
    };
    //////////////////////////////////////////////////////////////////////
    // XML load method
    //////////////////////////////////////////////////////////////////////
    MapHandler.prototype.loadXML = function(mapFile) {
    this.mapXML = new XML();
    this.mapXML.mapInstance = this;
    this.mapXML.ignoreWhite = true;
    this.mapXML.onLoad = this.traceXML;
    this.mapXML.load(mapFile);
    };
    //////////////////////////////////////////////////////////////////////
    // XML loading result method
    //////////////////////////////////////////////////////////////////////
    MapHandler.prototype.traceXML = function(success) {
    if (success) {
    //trace(this.mapInstance.mapXML);
    this.mapInstance.parseXML(this)
    } else {
    trace("XML connection error");
    }
    };
    //////////////////////////////////////////////////////////////////////
    // XML parce method
    //////////////////////////////////////////////////////////////////////
    MapHandler.prototype.parseXML = function(XMLData) {
    trace("parse you annoying git:"+XMLData);
    };
    //////////////////////////////////////////////////////////////////////
    // main
    //////////////////////////////////////////////////////////////////////
    defaultMapHandler = new MapHandler();
    defaultMapHandler.loadXML("mapXML_1.xml");



    [edit] Rereading the posts i got what your problem is,its the call of the parseXML method.
    I changed it so you see how it could be called while sticking as close as possible with the old syntax code you had.
    But as strille and some others said there are several things in there which aren´t done in ideal way, i suggest if you really want to get into doing things in oop manner you should check out AS2 or even better AS3 because there you get forced to do things the "right" way way more and some of those problems are automatically resolved then
    Last edited by tomsamson; 11-04-2006 at 03:32 PM.

  10. #10
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Yeah, your probably right, i've never been able to grasp object programming very well...I can sort of make my code neater using it but I know thats not its proper purpose. I've read a couple of tuts and perhaps i'm looking at it in a 'too complicated' way, but I get lost along the way every time...perhaps I need a good book, will check out the library tomorrow I think...

    Cheers for the help

    RipX

  11. #11
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666
    np mate and well,we´ve all been there and are still all day each day
    Imho you never suddenly are a great coder,its more an endless learning process and if one does a project and doesn´t get the thought of what he could do better in the next one something is wrong with the world anyway

    Regarding books i suggest you check out some by Collin Moock,always has been a good read to me

  12. #12
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Thanks Tom...just a final quick question...as an overview, how would you go about coding this instead? I notice a couple of people saying this isn't an ideal way of coding! If I was to use this in a scroll engine how would the classes be linked to such things as hitTesting, the scrolling etc...some example code would be perfect if you have time!

    Regards

    RipX

  13. #13
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    i would create your class structure like this:

    TileEngine (Xml loading, scrolling)

    Entity (collision with world, positoning, access to TileEngine's static scrolling function)

    Player extends Entity (player actions)

    StaticSprite extends Entity (objects, collectables)

    Enemy extends Entity (enemy actions)

    there are probably much better ways of doing it, i'm new to OOP as well, thats just how i'd do it
    lather yourself up with soap - soap arcade

  14. #14
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    yeah cool ...how would you call information to each of the classes using AS1..i.e. Entity having access to the TileEngine scrolling functions? I'm really basic with this stuff as you can see :| Would TileEngine be a SuperClass etc...sorry, just milking information about how this should be set up correctly..i'm sick of rewriting these things lol, and I really have to learn this stuff properly before i'm 74 years old and still using functions. lol

    RipX

  15. #15
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666


    You have one class as subclass of the other when you say this class extends the other (like malee did in several cases).
    So you extend one class with another if you really want to have a subclass which has all the methods (functions) and properties (variables) the superclass has.
    You normally start out with a very generic class and then extend that with more and more specific functionality featuring classes.
    In older flash versions that concept was harder to see with the core classes coming with flash,as most of them were already the very specific end subclass at the end of the class hierarchy chain.
    With AS3 the concept is way more notable with the classes coming with the dev api. Like there´s a generic displayObject class and then subclasses extending it in some way with custom functioanlity like a Sprite class for example.

    So to go back to male´s example,on the other side instead of extending a class with a subclass,you say object A knows object B or has a reference to it or has access to some methods of it when object A just has to know object B,not actually extend its class (note: object= instance of class ).
    Like in many cases you´d create a Model,the main logic handler class of your game/application. That one would have several helper classes but besides that act and be pretty autonomous.
    Anything view releated would know the model directly and also its methods,so be able to call those directly (not so clean) or just know the interface for how to apply for events and get added as listener for events it relies on.
    So to speak you could make the Model (logic side,seems to be "TileEngine" in male´s case) known to the view side in several ways. You could pass it in the constructor phase when instantiating a view object (like
    code:

    gameModel= new TileEngine()
    funkyEntity=new Entity(gameModel)



    or you could for example have a setter/getter set in the View Class,like
    code:

    function setModel(passedModel){
    this.model=passedModel
    }



    Then in the next step you have to implement a notifier system for your model so it can broadcast events (like Flash´s event dispatcher or simpler AS Broadcaster) and add view objects as listeners for the events they should react on.

    The whole sense of having abstract classes and then creating more specific subclasses is that you have your classes as reusable as possible.
    The sense of seperating logic (Model) and display (view) is similar,by not forcing your model to know the view side directly you can reuse it after exchanging the whole view side.
    So on the logicside you could write a racing game engine and on the view side showing all that data it could be a 2d topdown racing game view or equally a mode 7 view without having to change the logic side of the game.

    I kinda rushed over all points because well,i´m back to coding now but yup,those topics are a bit too indepth to be answered in the length they deserve in one forum post anyway,as i said a good idea is to get an indepth book on oop practices,well,hope it was any helpful anyway
    Last edited by tomsamson; 11-05-2006 at 02:51 AM.

  16. #16
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Ok guys...i'm pushing on with Flash 8 Pro which I got today - I have a quick one...I have written the following class named XMLHandler and pointed flash to the relevant class path:

    Code:
    import mx.utils.Delegate;
    class XMLHandler {
    	private var mapXMLData:XML;
    	//
    	function XMLHandler() {
    		// XML class constructor
    	}
    	public function mXMLLoad(url:String):Void {
    		mapXMLData = new XML();
    		mapXMLData.ignoreWhite = true;
    		mapXMLData.onLoad = Deligate.create(this, mXMLParse);
    		mapXMLData.load(url);
    	}
    	public function mXMLParse(success:Boolean):Void {
    		if (success) {
    			trace(mapXMLData);
    		} else {
    			trace("error loading xml file");
    		}
    	}
    }
    In the main timeline I have the following code:
    Code:
    iXMLHandler = new XMLHandler();
    iXMLHandler.mXMLLoad("mapXML_1.xml");
    It's not doing a thing so I was wondering if anyone could help with what i'm doing wrong?

    [EDIT]
    It's outputting the following error:
    Code:
    There is no method with the name 'Deligate'.
         		mapXMLData.onLoad = Deligate.create(this, mXMLParse);
    
    Total ActionScript Errors: 1 	 Reported Errors: 1
    RipX
    Last edited by RipX; 11-06-2006 at 05:52 PM.

  17. #17
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    You've spelt Delegate wrong.

    Squize.

  18. #18
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    lol...what a nutbar..working now cheers

    RipX

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center