PDA

View Full Version : repeated code



up-
July 6th, 2003, 12:58 PM
Ok, heres my problem... I have a movie clip with 30 buttons inside. Each button when pressed will load a var inside a txt file into a text field (b1 will load text1. b2 will load text2,etc....). Heres the code i have on my movie clip timeline: onClipEvent (enterFrame) {
b1.onPress = function() {
GetMethod = function () {
if (_url.substr(0, 4) == "file") {
delete myLoadVars.ranVariable;
return "POST";
} else {
return "GET";
}
};
myLoadVars = myLoadVars2=new LoadVars();
myLoadVars.ranVariable = new Date().getTime();
myLoadVars2.onLoad = function(success) {
if (success) {
_root.myText.html = true;
_root.myText.htmlText = this.text1;
} else {
trace("Error");
}
};
myLoadVars.sendAndLoad("textfile.txt", myLoadVars2, GetMethod());
};
Now here is my problem. Do I have to put that code for all 30 buttons??
There must be an easier way. Thanks in advance.

lostinbeta
July 6th, 2003, 01:16 PM
onClipEvent (enterFrame) {
b1.onPress = function() {
GetMethod = function () {
if (_url.substr(0, 4) == "file") {
delete myLoadVars.ranVariable;
return "POST";
} else {
return "GET";
}
};
myLoadVars = myLoadVars2=new LoadVars();
myLoadVars.ranVariable = new Date().getTime();
myLoadVars2.onLoad = function(success) {
if (success) {
_root.myText.html = true;
_root.myText.htmlText = this.text1;
} else {
trace("Error");
}
};
myLoadVars.sendAndLoad("textfile.txt", myLoadVars2, GetMethod());
};

Ok, time to analyze.

1) There is no need to run this onEnterFrame, just throw it on a frame and you should be fine.

2) There is no need to keep redefining the GetMethod function in every press.

3) There is no need to keep recreating the LoadVars() objects in ever press

4) There is no need to create 2 loadVars() objects.

5) I have no clue why you are using sendAndLoad if you are just loading in text... Unless I am missing something. I will work under the assumption you are not sending any information out. And under this assumption, there is no need for the GetMethod function, so we can remove that.

6) You don't need to keep telling the textbox to be html true in every press.

So that leaves us with....

_root.myText.html = true;
myLoadVars = new LoadVars();
b1.onPress = function() {
myLoadVars.onLoad = function(success) {
if (success) {
_root.myText.htmlText = this.text1;
} else {
trace("Error");
}
};
myLoadVars.load("textfile.txt?"+new Date().getTime());
};

I think that should work, I can't test it right now.

up-
July 6th, 2003, 01:28 PM
Hmm ok, but do i still have to have dat piece of code for every 30 buttons? Thx for the help.

lostinbeta
July 6th, 2003, 01:35 PM
Oh yeah, I forgot there was a tutorial on this :)

http://www.kirupa.com/developer/mx/multiple_dynamictext.htm

And yes, you will need the onPress code for all 30 buttons although you may be able to use a for loop to optimize your code. Something like this (untested)..

_root.myText.html = true;
var numberOfButtons = 30;
var myLoadVars = new LoadVars();
for (var i = 1; i<=numberOfButtons; i++) {
this["b"+i].varNum = i;
this["b"+i].onPress = function() {
myLoadVars.file = "text"+this.varNum;
myLoadVars.onLoad = function(success) {
if (success) {
_root.myText.htmlText = this[this.file];
} else {
trace("Error");
}
};
myLoadVars.load("textfile.txt?"+new Date().getTime());
};
}



It assumes your buttons names are b1, b2, b3, b4, etc, etc and your text variables names are text1, text2, text3, text4, etc, etc.

lostinbeta
July 6th, 2003, 01:42 PM
Sorry if it doesn't work, I am in a rush.

Actually I have to go now. Let me know how things work out. I will be back later.

up-
July 6th, 2003, 01:43 PM
No success.... :(

h88
July 6th, 2003, 02:09 PM
up: Looks working for me, though I guess you didn't understand what are the requirements of Lost's code. He said it already, what you exactly need is a set of TextFile variable names that looks like text1, text2, text3, text4, etc, etc. for each button that is named as b1, b2, b3, b4, etc and whenever each button is pressed, the myText textField would be replaced with the appropriate text for each button. Working now?

lostinbeta
July 6th, 2003, 03:24 PM
Thanks for checking it for me h88. And yes, all the text variables need to be in the same .txt file, seperated by an & symbol. So it would be like
text1=asdf
&text2=fffff
&text3=ggggggg
&text4=jjjjjj

Etc, etc.

lostinbeta
July 6th, 2003, 03:28 PM
Hrmm, now that I recheck you don't need to keep reloading the file either...


//set html to true for the textbox
_root.myText.html = true;
//set number of buttons
var numberOfButtons = 30;
//create loadVars object
var myLoadVars = new LoadVars();
//load file
myLoadVars.load("textfile.txt?"+new Date().getTime());
//after loading display default text
myLoadVars.onLoad = function(success) {
if (success) {
_root.myText.htmlText = this.text1
} else {
trace("Error");
}
};
//assign actions for each button
for (var i = 1; i<=numberOfButtons; i++) {
this["b"+i].varNum = i;
this["b"+i].onPress = function() {
_root.myText.htmlText = myLoadVars["text"+this.varNum];
};
}



If anyone could test that for me :beam:

up-
July 6th, 2003, 03:37 PM
Hey lost, i think it works. Can you explain this line for me?myLoadVars.load("news.txt?"+new Date().getTime());
Thanks a lot

lostinbeta
July 6th, 2003, 03:39 PM
myLoadVars.load("news.txt?"+new Date().getTime());

It prevents file cache by attaching a unique value to the end via a query string (the ? at the end of news.txt signifies a query string) so the browser thinks it is a new unique file. Being this is a news file, you would want people to see the newest most updated text, and unless they clear their temp file they won't, this prevents them having to do that.

lostinbeta
July 6th, 2003, 03:41 PM
Originally posted by h88
Works like a charm, Lost, :) and BTW up-, you should test it Online since it've got an extra dummy random variable in the load process that avoids caching.

Woo hoo :) I hate not being able to test my code, especially because I tend to make a lot of little mistakes.

Remember when you taught me loadVars() h88? That was a happy day for me indeed =)


oh, you deleted your post....now i'm just talking to myself

up-
July 6th, 2003, 03:42 PM
Thx lost and h88 for the great replies.
:)

h88
July 6th, 2003, 03:42 PM
Oh lol lost you always spot my posts before i delete them.

lostinbeta
July 6th, 2003, 03:43 PM
up- : No problem.

h88: Im quick like a bunny :beam: