00:00
00:00

Response to As: Xml 2008-07-14 10:02:56


At 7/14/08 07:46 AM, Hoeloe wrote: I have code exactly like the one you posted, but for some reason, whatever I try, Flash runs the function last, I used the debugger to find this. This means that I can't actually use what's I'm defining in an Array, which makes this all useless. What can I do?

You mean the event is triggered "last". This is because the load isn't done at compile time, THAT would make it useless. It's done on the fly, and therefore takes a few seconds. If you want to use the array, do so AFTER the even has triggered. For example, run whatever function it is you're using when the response comes back.

Don't call something useless just because you missed the point :P

If you still need help PM me (or post here)


- Matt, Rustyarcade.com

Response to As: Xml 2008-07-14 10:13:31


Messages = new XML();
Messages.ignoreWhite = true;
trace("This line traces first");
Messages.load('data.xml');
trace("Then this one traces");
Messages.onLoad = function(success) {
if (success) {
trace("The data should come just after this");
readMessages();
}
};
function readMessages(){
trace("and this");
_root.a=new Array();
for(i=0;Messages.firstChild.childNodes[i ]!=undefined;i++){
base=Messages.firstChild.childNodes[i].a ttributes.text;
_root.a.push(base);
}
_root.delay=0;
trace("done");
_root.onEnterFrame=function():Void{
if(_root.delay==0){
this.message=this.a[random(this.a.length )];
_root.delay=60;
}
_root.delay-=_root.delay>0;
}
}

trace(a);

That's my code. That traces as this:

This line traces first
Then this one traces
undefined
The data should come just after this
and this
done

The problem is that I can't continuously check for the array because I need to run other code when the data is loaded. In fact, I need to register the data to classes. This code isn't the one I'm using, but it is in essence the same thing, just the variable names are different and the xml file has been changed. My other code needs to run other event handlers and functions, so I can't just run it on an enterFrame. Got any tips?

PS, I didn't call XML useless, I just called my code useless, which was edited slightly from yours.

BBS Signature

Response to As: Xml 2008-07-14 10:15:56


you're tracing a before the onLoad has even happened. If you trace a in the readMessage function then you'll see it. What exactly is wrong with that?


- Matt, Rustyarcade.com

Response to As: Xml 2008-07-14 10:21:34


The reason I can't trace it in the function, is because my actual code needs to do more than just tracing an array. It needs to set the array, and then define classes and other things. But I can't do that all inside the function, because there are other functions to be defined that HAVE to be defined AFTER the array has. It's quiter complicated, but it just doesn't work. Is there no way to halt the code until the data has loaded?


BBS Signature

Response to As: Xml 2008-07-14 10:33:36


At 7/14/08 10:21 AM, Hoeloe wrote: It's quiter complicated, but it just doesn't work. Is there no way to halt the code until the data has loaded?

Put your application initialisation function call in the XML.onLoad() call


...

BBS Signature

Response to As: Xml 2008-07-14 10:35:33


At 7/14/08 10:21 AM, Hoeloe wrote: The reason I can't trace it in the function, is because my actual code needs to do more than just tracing an array. It needs to set the array, and then define classes and other things. But I can't do that all inside the function, because there are other functions to be defined that HAVE to be defined AFTER the array has. It's quiter complicated, but it just doesn't work. Is there no way to halt the code until the data has loaded?

I bet it's scoping issues. Thats probably why you are refusing to do as you're told :)
speak to me on msn (ninjachicken[at]hotmail.co.uk) and send me your code. I'll fix it


- Matt, Rustyarcade.com

Response to As: Xml 2008-07-14 10:39:09


I have you on msn already I think. Also, I have already tried what you asked and it hasn't worked. A third thing: I don't have msn on this computer, and I don't have the code on my home computer.


BBS Signature

Response to As: Xml 2008-07-14 10:40:24


At 7/14/08 10:39 AM, Hoeloe wrote: I have you on msn already I think. Also, I have already tried what you asked and it hasn't worked. A third thing: I don't have msn on this computer, and I don't have the code on my home computer.

gmail

rustygames[at]gmail.com

Or post it all here (in a code tag) if it isnt hugely long


- Matt, Rustyarcade.com

Response to As: Xml 2008-07-14 10:42:36


It's about 150 lines long, and that's excluding the external classes.


BBS Signature

Response to As: Xml 2008-07-14 10:45:32


At 7/14/08 10:42 AM, Hoeloe wrote: It's about 150 lines long, and that's excluding the external classes.

Paste it. And paste the external classes too. If they wont fit, www.pastebin.com and link us.

Or just zip em up and send them over to me via email


- Matt, Rustyarcade.com

Response to As: Xml 2008-07-14 10:47:46


_root.Questions = new Array();
QUESTIONS = new XML();
QUESTIONS.ignoreWhite = true;
trace("This line traces first");
trace("Then this one");
QUESTIONS.onLoad = function(success) {
	if (success) {
		trace("Read questions OK");
		readQuestions();
	} else {
		trace("Failed to read questions");
	}
};
QUESTIONS.load('Questionsxml.xml');
trace("And probably this!");
function readQuestions() {
	trace("Reading...");
	for (i=0; QUESTIONS.firstChild.childNodes[i] != undefined; i++) {
		newQ = QUESTIONS.firstChild.childNodes[i].attributes.text;
		newAnsa = QUESTIONS.firstChild.childNodes[i].attributes.ansa;
		newAnsb = QUESTIONS.firstChild.childNodes[i].attributes.ansb;
		newAnsc = QUESTIONS.firstChild.childNodes[i].attributes.ansc;
		newAnsd = QUESTIONS.firstChild.childNodes[i].attributes.ansd;
		newType = QUESTIONS.firstChild.childNodes[i].attributes.type;
		newAnsnum = QUESTIONS.firstChild.childNodes[i].attributes.ansno;
		newAnsone = QUESTIONS.firstChild.childNodes[i].attributes.ansone;
		newAnstwo = QUESTIONS.firstChild.childNodes[i].attributes.anstwo;
		newAnsthree = QUESTIONS.firstChild.childNodes[i].attributes.ansthree;
		newDiff = QUESTIONS.firstChild.childNodes[i].attributes.newDiff;
		_root["Qno"+i] = new Question(newQ, newAnsa, newAnsb, newAnsc, newAnsd, newType, newAnsnum, newAnsone, newAnstwo, newAnsthree, newDiff);
		_root.Questions.push(["Qno"+i]);
	}
	trace("Read.");
	_root.delay = 0;
	trace("Done.");
	_root.onEnterFrame = function():Void  {
		if (_root.delay == 0) {
			this.message = this.newQ[random(this.newQ.length)];
			_root.delay = 60;
		}
		_root.delay -= _root.delay>0;
	}
}
score = 0;
corrans = 0;
qNum = 1;
Array.prototype.findDifficulty = function(Element):Array  {
	list = new Array();
	for (var i = 0; i<this.length; i++) {
		if (this[i].getDiff() == Element) {
			list.push(i);
		}
	}
	return list;
};
function testQuestion(number):Void {
	questionNum = number-1;
}
Array.prototype.findDupe=function(): Boolean{
	for(var i=(this.length-1); i>0; i--){
		for(var j=i-1; j>-1; j--){
			if(this[i]==this[j]){ 
			return true;
		}else{
			return false;
		}
		}
	}
}
_root.AnssA = false;
_root.AnssB = false;
_root.AnssC = false;
_root.AnssD = false;
EASY = Questions.findDifficulty("Easy");
MED = Questions.findDifficulty("Med");
HARD = Questions.findDifficulty("Hard");

EasyQ = 2;
MedQ = 2;

EasyQ--;
qnumba = random(EASY.length);
questionNum = EASY[qnumba];
qno = "Q"+qNum;

// _root.testQuestion(Replace this text with a question number);

questions = Questions[questionNum].getQuestion();
layout.anA = Questions[questionNum].getOptionA();
layout.anB = Questions[questionNum].getOptionB();
layout.anC = Questions[questionNum].getOptionC();
layout.anD = Questions[questionNum].getOptionD();
layout.gotoAndStop(Questions[questionNum].getType());
rightAns = Questions[questionNum].getAns()[0];
rightAnsb = Questions[questionNum].getAns()[1];
rightAnsc = Questions[questionNum].getAns()[2];
_root.Ansleft = _root.Questions[_root.questionNum].getAnsNum();
function newQuestion():Void {
	_root.Questions.splice(_root.questionNum,1);
	_root.EASY = _root.Questions.findDifficulty("Easy");
	_root.MED = _root.Questions.findDifficulty("Med");
	_root.HARD = _root.Questions.findDifficulty("Hard");
	if (_root.EasyQ == 0 && _root.MedQ == 0 || _root.MED[0] == undefined && _root.EASY[0] == undefined) {
		_root.qnumba = random(_root.HARD.length);
		_root.questionNum = _root.HARD[_root.qnumba];
	} else if (_root.MedQ>0 && _root.EasyQ == 0 || _root.MedQ>0 && _root.EASY[0] == undefined) {
		_root.MedQ--;
		_root.qnumba = random(_root.MED.length);
		_root.questionNum = _root.MED[_root.qnumba];
	} else if (_root.EasyQ>0) {
		_root.EasyQ--;
		_root.qnumba = random(_root.EASY.length);
		_root.questionNum = _root.EASY[_root.qnumba];
	}
	_root.qNum++;
	_root.questions = _root.Questions[_root.questionNum].getQuestion();
	_root.layout.anA = _root.Questions[_root.questionNum].getOptionA();
	_root.layout.anB = _root.Questions[_root.questionNum].getOptionB();
	_root.layout.anC = _root.Questions[_root.questionNum].getOptionC();
	_root.layout.anD = _root.Questions[_root.questionNum].getOptionD();
	_root.layout.gotoAndStop(_root.Questions[_root.questionNum].getType());
	_root.rightAns = _root.Questions[_root.questionNum].getAns()[0];
	_root.rightAnsb = _root.Questions[_root.questionNum].getAns()[1];
	_root.rightAnsc = _root.Questions[_root.questionNum].getAns()[2];
	_root.Ansleft = _root.Questions[_root.questionNum].getAnsNum();
	_root.AnssA = false;
	_root.AnssB = false;
	_root.AnssC = false;
	_root.AnssD = false;
	_root.qno = "Q"+_root.qNum;
}

.AS File:

class Question {
	var question:String;
	var OptionA:String;
	var OptionB:String;
	var OptionC:String;
	var OptionD:String;
	var Type:String;
	var AnswerNo:String;
	var AnswerA:String;
	var AnswerB:String;
	var AnswerC:String;
	var Difficulty:String;
	function Question(param_question:String, param_OptionA:String, param_OptionB:String, param_OptionC:String, param_OptionD:String, param_Type:String, param_AnswerNo:String, param_AnswerA:String, param_AnswerB:String, param_AnswerC:String, param_Difficulty:String) {
		this.question = param_question;
		this.OptionA = param_OptionA;
		this.OptionB = param_OptionB;
		this.OptionC = param_OptionC;
		this.OptionD = param_OptionD;
		this.Type = param_Type;
		this.AnswerNo = param_AnswerNo;
		this.AnswerA = param_AnswerA;
		this.AnswerB = param_AnswerB;
		this.AnswerC = param_AnswerC;
		this.Difficulty = param_Difficulty;
	}
	function getQuestion():String {
		return question;
	}
	function getOptionA():String {
		return OptionA;
	}
	function getOptionB():String {
		return OptionB;
	}
	function getOptionC():String {
		return OptionC;
	}
	function getOptionD():String {
		return OptionD;
	}
	function getType():String {
		return Type;
	}
	function getAnsNum():String {
		return AnswerNo;
	}
	function getDiff():String {
		return Difficulty;
	}
	function getAns():Array {
		return new Array(AnswerA, AnswerB, AnswerC);
	}
	function setQuestion(thing):String {
		return question=thing;
	}
	function setOptions(thinga, thingb, thingc, thingd):String {
		return OptionA=thinga, OptionB=thingb, OptionC=thingc, OptionD=thingd;
	}
	function setType(thingy):String {
		return Type=thingy;
	}
	function setAns(thingz, thingzz, thingzzz):String {
		return AnswerA=thingz, AnswerB=thingzz, AnswerC=thingzzz;
	}
	function setAnsNum(lol):String {
		return AnswerNo=lol;
	}
}

Thar.


BBS Signature

Response to As: Xml 2008-07-14 10:50:38


and the xml?


- Matt, Rustyarcade.com

Response to As: Xml 2008-07-14 10:53:43


<?xml version="1.0" ?>
<data>
<Message text="Welcome to Godlimation" ansa="wth?" ansb = "lolphail!" ansc= "meh!" ansd = "!!!!!!111111111111one111!!!" type = "Text" ansno = "2" ansone = "A" anstwo = "B" ansthree = "C" diff = "Easy"/>
<Message text="Site Created By Inglor" ansa="wth?" ansb = "lolphail!" ansc= "meh!" ansd = "!!!!!!111111111111one111!!!" type = "Text" ansno = "2" ansone = "A" anstwo = "B" ansthree = "C" diff = "Easy" />
<Message text="This site belongs to Patrick, creator of Xunmato"  ansa="wth?" ansb = "lolphail!" ansc= "meh!" ansd = "!!!!!!111111111111one111!!!" type = "Text" ansno = "2" ansone = "A" anstwo = "B" ansthree = "C" diff = "Easy"/>
<Message text="Visit our new link section"  ansa="wth?" ansb = "lolphail!" ansc= "meh!" ansd = "!!!!!!111111111111one111!!!" type = "Text" ansno = "2" ansone = "A" anstwo = "B" ansthree = "C" diff = "Easy"/>
<Message text="The user system, is N/A yet"  ansa="wth?" ansb = "lolphail!" ansc= "meh!" ansd = "!!!!!!111111111111one111!!!" type = "Text" ansno = "2" ansone = "A" anstwo = "B" ansthree = "C" diff = "Easy"/>

</data>

Nothing hardcore in there.


BBS Signature

Response to As: Xml 2008-07-15 05:13:05


At 7/14/08 10:47 AM, Hoeloe wrote: newDiff = QUESTIONS.firstChild.childNodes[i].attri butes.newDiff;

I fixed it. My error was there. It needed to be typed like this:

newDiff = QUESTIONS.firstChild.childNodes[i].attributes.diff;

Plus I put all the code in an EnterFrame with a variable so that it only ran once, and defined all the functions before loading the XML.


BBS Signature

Response to As: Xml 2008-07-15 05:16:18


Ah yea I fixed this yesterday and forgot to post. What I did was put your init code into an "init" function then called that from when the XML was loaded.


- Matt, Rustyarcade.com

Response to As: Xml 2008-07-15 05:21:15


Good idea. Well, both ways work, so whatever :P


BBS Signature

Response to As: Xml 2008-07-15 06:56:41


At 7/15/08 05:21 AM, Hoeloe wrote: Good idea. Well, both ways work, so whatever :P

Kayn mentioned this quite a while ago...

You should probably group your functions together in one place and separate them from the code outside the functions. The code outside of them will run procedurally, calling the defined functions when it needs to. Separating them visually should help--but you should also try to factor them out into their own compilation units when possible.

As well, "both techniques" work fine, but they should be used in tandem generally. The 'flag' approach is good (given you remove the event listener after your code has been executed) as it allows for multiple asynchronous events to finish up before "initialization" happens--and the separate init function helps clarity and design as it separates the initialization procedure from anything else that may need to happen, as well as labeling its purpose.


We should take care not to make the intellect our god; it has, of course, powerful muscles, but no personality.

Freshmaking

Brainscrape

BBS Signature

Response to As: Xml 2008-07-15 07:26:34


Sorry about all these questions, I am rather useless at this kind of thing. You've covered how to extract data from an XML file into flash, but what if I need to send data to the XML file from flash? I understand pretty well how to recieve, but when sending data, I'm hopeless.


BBS Signature

Response to As: Xml 2008-07-15 08:12:21


Sorry about all these questions, I am rather useless at this kind of thing. You've covered how to extract data from an XML file into flash, but what if I need to send data to the XML file from flash? I understand pretty well how to recieve, but when sending data, I'm hopeless.

You can alter the XML data in memory, but unfortuantely Flash isn't capable of writing to files, so you'll have to use another language as a proxy. There are a number of ways to do that, but it would depend on your knowledge and particular situation. In many cases, there may be a better (or just different) solution to whatever it is you're trying to do.


We should take care not to make the intellect our god; it has, of course, powerful muscles, but no personality.

Freshmaking

Brainscrape

BBS Signature

Response to As: Xml 2008-07-15 08:25:44


Not write, send, like to servers.
The XML class has a send method?


BBS Signature

Response to As: Xml 2008-07-15 08:41:42


Nah, he's right. I need it to write to an XML file so that I can retrieve it later. I'll just use a different language, thanks though :P


BBS Signature

Response to As: Xml 2008-07-25 05:53:24


is there a way to load an XML file into SWF if the XML file is uploaded on another server ?

Response to As: Xml 2008-07-25 06:31:04


At 7/25/08 05:53 AM, Argentin wrote: is there a way to load an XML file into SWF if the XML file is uploaded on another server ?

The server you're trying to get the file from needs a Cross Domain XML Policy file (named crossdomain.xml) in the root of the server. This is a core part of Flash and can not be worked around (well... not directly through Flash anyway...). It stops hotlinking to xml files.

A sample crossdomain.xml is available here (http://xml.amazon.com/crossdomain.xml) that allows connections from everywhere. You'll need the server admin there to add you to the permissions list, or to allow all sites to connect to it.


More info on crossdomain.xml here


...

BBS Signature