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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Actionscript: Help with making Reusable Code 1

Status
Not open for further replies.

rockstardub

Programmer
Dec 29, 2004
42
US
I have a problem, I need to learn how to make my code more reusable, heres what I'm working with:
Code:
	if(_root.news_select == 1) {
		if(homenews.topic2._currentframe == 10) { //if it's red
			homenews.topic2.play();
		}
		if(homenews.topic1._currentframe == 1) { //if it's black
			homenews.topic1.play();
		}
		if(homenews.topic3._currentframe == 10) { //if it's red
			homenews.topic3.play();
		}
	} else if(_root.news_select == 2) {
		trace(TCurrentLabel(homenews.topic1));
		if(homenews.topic1._currentframe == 10) { //if it's red
			homenews.topic1.play();
		}
		if(homenews.topic2._currentframe == 1) { //if it's black
			homenews.topic2.play();
		}
		if(homenews.topic3._currentframe == 10) { //if it's red
			homenews.topic3.play();
		}
	} else if(_root.news_select == 3) {
		if(homenews.topic2._currentframe == 10) { //if it's red
			homenews.topic2.play();
		}
		if(homenews.topic3._currentframe == 1) { //if it's black
			homenews.topic3.play();
		}
		if(homenews.topic1._currentframe == 10) { //if it's red
			homenews.topic1.play();
		}
	}

I made an attempt by trying this, but I can't get it to work, or any other form of it.

Code:
	for (i=1; i >= 3; i++) {
		if(_root.news_select == i) {
			if("homenews.topic" + i + "._currentframe" == 1) { //if it's black
				"homenews.topic" + i + ".play()"; // make it red
			}
		} else {
			if("homenews.topic" + i + "._currentframe" == 10) { //if it's red
				"homenews.topic" + i + ".play()"; // make it black
			}
		}
	}

Please Help!
Thanks,
Rockstar
 
well for starters, your for loop will never even be processed because you have i = 1; then i >=3. I think you meant <= 3. Try that and see what it does.


Keith


The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents.
 
Oops, well I think this is my real problem that I've faced more than once...

"homenews.topic" + i + "._currentframe"

concatenating these things, how is it done? eval() or what?
 
hey great syntax, thanks, haven't seen that anywhere, I'll give it a try.
 
I don't know what the problem is, I tried to duplicate this code:

Code:
	homenews.topic1.header.header = _root.newsheader1 + " # 1 /";
	homenews.topic2.header.header = _root.newsheader2 + " # 2 /";
	homenews.topic3.header.header = _root.newsheader3 + " # 3 /";

into this, but it didn't work

Code:
     for (i=1; i<=3; i++) {
		["homenews.topic" + i].header.header = _root.["newsheader" + i] + " # " + i + " /";
	}

I get this error
"Expected a field name after '.' operator.
 
Code:
for (i=1; i<=3; i++) {
        ["homenews.topic.header.header" + i] = _root.["newsheader" + i] + " # " + i + " /";
    }

although unless you are getting a value like the currentframe this would do

for (i=1; i<=3; i++) {
        "homenews.topic.header.header" + i = _root."newsheader" + i + " # " + i + " /";
    }
 
That can't work though, because the topic needs a number after it.

homenews.topic1.header.header = _root.newsheader1 + " # 1 /";

 
Look, I've made a more simplified version of the problem, please try and work with the FLA and show me what I'm doing wrong.

Download this simplified FLA

I commented part of the code so it would work when I tested the FLA,
so I knew I could do part of it right, but still cannot make it reusable.

Code:
//This Works
multitext.sub1.text = "new";
multitext.sub2.text = "new";
multitext.sub3.text = "new";


//This does not, but this is what I want to do!
/*

for (i=1; i<=3; i++) {
	"multitext.sub" + i + ".text" = "new";
}

*/
 
Thanks man,
helped a great deal,so what is the explination behind the period missing?
does the "[" tag lose the period in the start, and why not the end as well?
Is that similar to an array of the movie clip?
Is that how the information is stored?
 
all languages have syntax and in as1/2 this is the simplest way (sometimes) to access an objects properties

there are lots of examples on the forum to look at

i can see the confusion between

clipped.clip1.text = "2b5;

and , where x = 1

clipped["clip"+ x].text = "2b5"

all the [] bracket is doing is saying look inside clipped object/movieclip, find clip + x, then set its text value to 2b5

the syntax of flash demands that the value be preceeded by a . (or ._ ie ._y) but when referencing the object itself you may or may not use dot syntax.


ok i give up....that explanation is about as clear as mud
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top