Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

(Help) Simple Visible/unvisible movieclip with external var

Status
Not open for further replies.

keish

Technical User
Apr 1, 2007
7
PT
I need some help from you guys. I don't have much knowledge of Actionscript yet so my question might be simple to

solve.

I have a fla with a dynamic textbox instanced as box_txt and a movieclip instanced as movie_mc. Then I have an

external txt file named text.txt with only one var: &stat. This var can be either Visible or Unvisible.

What I want it's just two things. First to load the var into the the textbox and second to make the movieclip

visible or unvisible.

Load the var from the text file to the textbox it's easy but simultaneously make the movieclip visible/unvisible

it's being much more dificult to me.

I supose there is lots of (easy) ways to do this but I don't know how. Based on the code that some of you gave me on

a previous post, I tried the following (single block code on frame 1):




myData = new LoadVars();
myData.onLoad = function(success) {
if (success) {
box_txt.htmlText = this.stat;
switch (this.stat) {
case "Visible" :
_root.movie_mc.visible = true
break;
case "Unvisible" :
_root.movie_mc.visible = false
break;
}
}

else {
box1_txt.htmlText = "error";

}
};

myData.load("text.txt");
stop();

++++++++++++++++++++ I Also tried this one:


myData = new LoadVars();
myData.onLoad = function(success) {
if (success) {
box_txt.htmlText = this.stat;
moveClip();
}
else {
box_txt.htmlText = "error";

}
};
function moveClip(){
if (box_txt.htmlText == "Visible") {
movie_mc._visible = true;
}
else if (box_txt.htmlText == "Unvisible") {
movie_mc._visible = false;
}
}
myData.load("text.txt");
stop();


There's no error message on the code but nothing happens to the movieclip. Can u help me?

Thx in advance
 
Your first script is fine apart from "visible" should be "_visible".

The reason why it's not working is (I guess) your text file has unwanted characters in it.

"&stat=Unvisible"

and

"&stat=Unvisible
"

are not the same as the second one has two extra (invisible) characters at the end: Carriage Return and Line Feed (if you're using Windows). You can do "&stat=Unvisible&" to make sure it ends with nothing.

("Invisible", not "Unvisible" by the way.)

Kenneth Kawamoto
 
Ok, u were right. The problem was with the txt file. I didn't have the & at the end of each variable.

I now have:
text.txt:

&stat=Alison&
&stat2=Rute&
&stat3=Cameron&
&stat4=Eugene&
&stat5=Rita&


I still have some questions though. The code you gave me works fine with one textbox and one movieclip but what if I want like 5 textbox and 3 movieclips and the movieclips position's depending of the variable value of 3 diferent textboxs?. I tried to had the remaining code but with no success.



the two codes I have that work are:

_root.movie_mc._visible = false;
_root.movie2_mc._visible = false;
_root.movie3_mc._visible = false;

myData = new LoadVars();
myData.onLoad = function(success) {
if (success) {
box_txt.text = this.stat;
switch (this.stat) {
case "Alison" :
_root.movie_mc._visible = true
_root.movie_mc._x = 250
_root.movie_mc._y = 200
break;
case "" :
_root.movie_mc._visible = false
break;
};
}
else {
box1_txt.htmlText = "error";

}
};

myData.load("text.txt");
stop();

++++++++++++++++++++++++++++++++++++++++++++

_root.movie_mc._visible = false;
_root.movie2_mc._visible = false;
_root.movie3_mc._visible = false;

myData = new LoadVars();
myData.onLoad = function(success) {
if (success) {
box_txt.text = this.stat;
box2_txt.text = this.stat2;
box3_txt.text = this.stat3;
box4_txt.text = this.stat4;
box5_txt.text = this.stat5;

moveClip();
}
else {
box_txt.text = "error";

}
};
function moveClip(){
if (box_txt.text == "Alison") {
_root.movie_mc._visible = true;
_root.movie_mc._x = 200
_root.movie_mc._y = 100
}
else if (box_txt.text == "") {
_root.movie_mc._visible = false;
}
}
myData.load("text.txt");
stop();

+++++++++++++++++++++++++++++++++++++++++

The method for the other movieclips positioning should be similar, for example:

if (box2_txt.text == "Rute") {
_root.movie_mc._visible = true;
_root.movie2_mc._x = 150
_root.movie2_mc._y = 85
}
else if (box2_txt.text == "") {
_root.movie_mc._visible = false;
}

I just need to know how to add the remaining code to get this working.


Thx once again


PS: sorry about the grammar errors but english is not my mother language
 
Your text file is not right. Also I would change the variable "stat" to "stat1" and the TextField "box_txt" to "box1_txt" so that all of them are nicely numbered. If all of them are numbered you can code things much easier and more elegantly.

Text file:
Code:
stat1=Alison&stat2=Rute&stat3=Cameron&stat4=Eugene&stat5=Rita

AS:
Code:
myData = new LoadVars();
myData.root = this;
myData.onLoad = function(success) {
	if (success) {
		for (var i = 1; i<=5; i++) {
			this.root["box"+i+"_txt"].text = this["stat"+i];
		}
	} else {
		box1_txt.text = "error";
	}
};
myData.load("text.txt");

Kenneth Kawamoto
 
I will do like u told me but for now the problem is with the positioning of the movieclips but let me show you the big picture:

I have several friends that go abroad all the time. Im creating a fla to see where they are.

When they are out somewhere, I register him/her on an excel file and output that info into the txt file (I've created a button with vba, no big deal).

The output txt file looks like this:
&friend1=Julia&
&location1=Milan&
&age1=23&
&phone1=&
&friend2=Andrea&
&location2=Cairo&
&age2=20&
&phone2=4543435435&
&friend3=Bruno&
&location3=Buenos Aires&
&age3=25&
&phone3=&

etc


In the fla I have a flag (movie clip) for each of my friends (and for this particular case, lets imagine that my friends aren't always the same).


So I have 4 textboxs and 1 flag for each friend. Once their're abroad the txt file is update (manually through excel).

In the fla I'll have a world map and the textboxs (4 for each friend at a maximum of 5 friends at the same time) and 1 flag for each friend. This flag positioning will be set by the coordinates I have on the code. My friend's always go to same places so I don't have to set that many coordinates. Its something like this:


_root.draw1_mc._visible = false
_root.draw2_mc._visible = false
//etc

myData = new LoadVars();
myData.onLoad = function(success) {
if (success) {
friend1_txt.text = this.friend1;
position1_txt.text = this.position1;
age1_txt.text = this.age1;
phone1_txt.text = this.phone1;
friend2_txt.text = this.friend2;
position2_txt.text = this.position2;
age2_txt.text = this.age2;
phone2_txt.text = this.phone2;
switch (this.position1) {
case "Milan" :
_root.flag1_mc._x = 250
_root.flag1_mc._y = 200
_root.draw1_mc._visible = true
break;
case "Rome" :
_root.flag1_mc._x = 207
_root.flag1_mc._y = 722
_root.draw1_mc._visible = true
break;
case "" :
_root.flag1_mc._visible = false
_root.draw2_mc._visible = false
_root.draw1_mc._visible = true
break;
}
switch (this.position2) {
case "Cairo" :
_root.flag2_mc._x = 312
_root.flag2_mc._y = 130
_root.draw2_mc._visible = true
break;
case "Tunis" :
_root.flag2_mc._x = 207
_root.flag2_mc._y = 120
_root.draw2_mc._visible = true
break;
case "" :
_root.flag2_mc._visible = false
_root.draw2_mc._visible = false
break;
}
// etc

}

else {

}
};

myData.load("text.txt");
stop();


But the problem is that it doesn't work. Sometimes I managed to move the flag with the code for just one friend and only one positioning but once I add the code for the rest, everything stops working

PS: the draw1 and draw2 are just mc's with an image.
 
It's the same - I'd do it like the one in my previous post:
Code:
var coordinates = new Object();
coordinates["Milan"] = [250, 200];
coordinates["Rome"] = [207, 722];
coordinates["Cairo"] = [312, 130];
coordinates["Tunis"] = [207, 120];
//
myData = new LoadVars();
myData.root = this;
myData.onLoad = function(success) {
	if (success) {
		for (var i = 1; i<=5; i++) {
			this.root["friend"+i+"_txt"].text = this["friend"+i];
			this.root["position"+i+"_txt"].text = this["position"+i];
			this.root["age"+i+"_txt"].text = this["age"+i];
			this.root["telephone"+i+"_txt"].text = this["telephone"+i];
			var _position = this["position"+i];
			if (_position.length) {
				with (this.root["flag"+i+"_mc"]) {
					_x = coordinates[_position][0];
					_y = coordinates[_position][1];
				}
			}
			this.root["draw"+i+"_mc"]._visible = (_position.length>0);
		}
	} else {
		box1_txt.text = "error";
	}
};
myData.load("text.txt");
You're mixing "location" and "position" - I used "position" in the example above.

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top