View Full Version : (FMX) Load external and select random
chipdouglas
July 19th, 2003, 06:09 AM
Hi there!
How do I load a external file and then display a random var (fact) from it??
this is what I got :-)
-------------AS--------------------------
loadVariablesNum("data.txt", 0);
------------------------------------------
-------------external file-------------
&fact1=hi there&fact2=hello
-----------------------------------------
The number of facts in the external file is random, so some dynamic stuff would be cool...total newbie :-)
:beam:
/ChipDouglas
http://www.chipdouglas.dk
kode
July 19th, 2003, 06:25 AM
Use the LoadVars object instead of loadVariables/loadVariablesNum to load the TXT file.
lv = new LoadVars();
facts = new Array();
lv.onLoad = function(success) {
if (success) {
for (var prop in this) {
facts[facts.length] = this[prop];
}
} else {
trace("error");
}
};
ASSetPropFlags(lv, "onLoad", 1, 0);
lv.load("data.txt");
To get a random variable:
var fact = facts[Math.floor(Math.random()*facts.length)];
trace(fact);
LoadVars object: http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary427.html
Array object: http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary059.html
Math object: http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary446.html
chipdouglas
July 19th, 2003, 06:36 AM
It does'nt seem to work..or i'm just confused...i'm a totally newbie so a .fla file would really help me :-)
chipdouglas
July 19th, 2003, 06:41 AM
-----------this works--------------------
lv = new LoadVars();
facts = new Array();
lv.onLoad = function(success) {
if (success) {
for (var prop in this) {
facts[facts.length] = this[prop];
}
} else {
trace("error");
}
};
ASSetPropFlags(lv, "onLoad", 1, 0);
lv.load("data.txt");
----------------------------------------------
----------this doesn't----------------------
var fact = facts[Math.floor(Math.random()*facts.length];
trace(fact);
----------------------------------------------
get "undefined"
/ChipDouglas
kode
July 19th, 2003, 06:43 AM
You are just confused... :P
Get the random variable after the external file has been loaded. That was just an example. :sigh:
chipdouglas
July 19th, 2003, 06:49 AM
*mumbling with a gun in my mouth* plz help me!
kode
July 19th, 2003, 06:56 AM
I already told you how to do it, give me more details so I can help you better.
What do you want to do exactly with the variable?
chipdouglas
July 19th, 2003, 06:59 AM
i have a lots fact in sentence like "Copenhagen is the capital of Denmark"....I want them to be loaded from the external file a be random displayed in a dynamic text box..thats about it...does it make sence??
/Chip
kode
July 19th, 2003, 07:24 AM
lv = new LoadVars();
lv.onLoad = function(success) {
if (success) {
var prop = null, facts = [];
for (prop in this) {
facts[facts.length] = this[prop];
}
myTextFieldInstanceName.text = facts[Math.floor(Math.random()*facts.length)];
} else {
trace("error");
}
};
ASSetPropFlags(lv, "onLoad", 1, 0);
lv.load("data.txt");
Just replace myTextFieldInstanceName with the actual instance name of your TextField.
chipdouglas
July 19th, 2003, 07:29 AM
in the words of Tony the Tiger: "Grrrrrrrrrreaat!" :-) it works and I found an error ;-) a missing ")" in
facts[Math.floor(Math.random()*facts.length];
thanks a lot :-)
kode
July 19th, 2003, 07:32 AM
A little typo. Yeah, I shouldn't write the code in the quick reply box (although I wrote it correctly in my last reply). :P
And you're welcome. =)
eyezberg
July 19th, 2003, 09:15 AM
wtf is
ASSetPropFlags(lv, "onLoad", 1, 0);
for?
kode
July 19th, 2003, 10:13 AM
To hide the onLoad handler. Otherwise you'd also the store the function in the Array when the for..in action is executed. :P
http://chattyfig.figleaf.com/flashcoders-wiki/index.php?ASSetPropFlags
You could also use something like this:
for (prop in this) {
if (typeof this[prop] != "function") {
facts[facts.length] = this[prop];
}
}
//
for (prop in this) {
if (!this[prop] instanceof Function) {
facts[facts.length] = this[prop];
}
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.