Topic: Flash, XML, class constructors (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=11461" title="Pages that link to Topic: Flash, XML, class constructors (Page 1 of 1)" rel="nofollow" >Topic: Flash, XML, class constructors <span class="small">(Page 1 of 1)</span>\

 
Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 10-12-2003 02:57

I'm just now learning Flash, and I'm having a problem that might have an obvious solution. I'm creating a class which gets an XML file from my PHP backend, keeps it as a Flash XML object, and doles out relevant information when requested. It looks like this:

code:
class GameData
{

[i]// instance variables[/i]
private var game_xml;

[i]// constructor[/i]
function GameData(url)
{
game_xml = new XML();
game_xml.ignoreWhite = true;
game_xml.onLoad = gameOnLoad;
game_xml.load(url);
trace("Loading XML file.");
}

function gameOnLoad(success) [i]// variable [b]success[/b] is sent by event[/i]
{
if (success)
trace("XML file contents: " + game_xml.toString());
}

[i]// rest of class, mostly getThing() functions that request specific nodes[/i]
}



According to every Flash/XML tutorial and document I can find, this should work. The XML.load() function should put the contents of the URL variable into the XML object (game_xml in this case), and then game_xml.toString() should return a text representation of the XML doc. This does not happen for me.

Instead, I have to do this (only relevant parts shown):

code:
[i]// this is in the constructor[/i]
game_xml.onLoad = function(success)
{
if (success)
{
game_xml = this;
}
}
game_xml.load(url);



And then everything works great. The trouble is, I haven't seen that thing about setting game_xml equal to itself (since, if I'm right, this called from game_xml.onLoad() is in fact game_xml)...

So what am I doing wrong? Or if I'm not doing something wrong, where's the hole in my understanding? I'm going to proceed empirically, but I'd like to know the theory.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-12-2003 03:34

Just a guess, but maybe it should be

game_xml.onLoad = this.gameOnLoad;

?

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 10-12-2003 21:02

This would be Actionscript 2 (Flash MX 2004) yes?

You should probably mention that next time, as the class constructor threw me at first (didn't have em pre MX 2004).

Anyways, slime is correct, you should specify this so that flash knows to look for function gameOnLoad() inside the GameData object instance. As it currently is, the XML onLoad event is looking for a global gameOnLoad() function, which doesn't exist.

As for setting game_xml to this well, you may as well be setting game_xml = game_xml as this inside of an XML object method refers to the XML object instance, which is game_xml. So there's really no need for it.

Edit: Inside the gameOnLoad method, it's preferable to just use this to refer to the XML data: this.firstChild.childNodes[0].attributes.first_name etc ect...



[This message has been edited by Dracusis (edited 10-12-2003).]

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 10-12-2003 22:52

Well, here's the weird thing: as written in the first example, the gameOnLoad() function was working, even specified as "game_xml.onLoad = gameOnLoad" in the constructor!

Dracusis, good comment -- you're entirely correct. I removed the redundant assignment operation, and the XML file still gets loaded into game_xml (which is good, because that's how it should work anyway).

What's more, asking for this.toString() within the gameOnLoad function works, but game_xml.toString() doesn't. I guess I didn't understand the scoping... Within a method of game_xml, I can't refer to the parent class object by its parent class identifier.

Well, now I get it. Thanks for the help, gentlemen! I'll probably have more questions, but on the other hand I'm working on implementing a web-based game that I think you'll really like when I'm done!

Cell 1250 :: alanmacdougall.com :: Illustrator tips

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 10-13-2003 04:10

I'm glad things are working now

I look forward to seeing the finished product. Flash games are always a welcome addition to the web =).

pix303
Obsessive-Compulsive (I) Inmate

From:
Insane since: Jan 2005

posted posted 01-04-2005 10:39

look at this very interesting macromedia link

http://livedocs.macromedia.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00002877.html

Paolo
www.valentinadorme.it

Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 01-04-2005 19:30

If you have the 7.2 (elipsis) update, there is a very useful new class built in called "Delegate" which really helps a lot with scoping things like callback functions.

Also, adding datatypes to variables and functions REALLY helps me track down my all too frequent bone-headed booboos. I would rewrite your code as follows:

code:
import mx.utils.Delegate;

class GameData {

private var game_xml:XML;

// constructor
function GameData(url:String):Void {
game_xml = new XML();
game_xml.ignoreWhite = true;
game_xml.onLoad = Delegate.create(this, gameOnLoad);
game_xml.load(url);

}

private function gameOnLoad(success:Boolean):Void {
// variable success is sent by event
if (success) {
trace("XML file contents: " + game_xml.firstChild.toString());
} else {
trace ("document failed to load");
}
}

// rest of class, mostly getThing() functions that request specific nodes

}



So "this" (the class itself) is passed to the callback, and since game_xml is an instance variable the callback can find it no sweat. Google: Flash+Delegate and you will find a lot of info. Some of it digestable, some not. It sure can help with scope issues in class though. One by Senocular is excellent, and it appears in a more comprehensive tutorial at Kirupa. Very good stuff.

(Edited by Steve on 01-04-2005 19:39)



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu