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

help with a for loop

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
I found some code on the macromedia website that creates a star by drawing it. What I want to do is make 5 of them and have each on a different link. here is the code

d_mc = _root.createEmptyMovieClip("draw_layer", 2);
function star() {
d_mc.clear();
d_mc.lineStyle(2, 0x6688AA);
d_mc.beginFill(0xFF0000, 100);
d_mc.drawStar(100, 75, 5, 25, 50, 90);
d_mc.endFill();
}
star();
draw_layer.onRelease=function(){
getURL(" + i,"_blank","POST");
}
draw_layer._x = 100
draw_layer._y = 100

I have tried it like this, but now no stars appear . What might I be doing wrong.

for(i=1;i<5;i++){
d_mc = _root.createEmptyMovieClip(&quot;draw_layer&quot;+i, 2);
function star() {
d_mc.clear();
d_mc.lineStyle(2, 0x6688AA);
d_mc.beginFill(0xFF0000, 100);
d_mc.drawStar(100, 75, 5, 25, 50, 90);
d_mc.endFill();
}
star();
[draw_layer].onRelease=function(){
getURL(&quot; + i,&quot;_blank&quot;,&quot;POST&quot;);
}
draw_layer._x = 100 * i
draw_layer._y = 100 * i
}
 
That 'star' function is calling another function which you haven't included called 'drawStar' which would explain why nothing's appearing.

Once you've got that included though you'll need to make a few changes before it works the way you want.

1)You don't need to include the function in the for loop, only the call to the function.

2) Each empty clip needs to be created on its own layer so they don't overwrite each other, so it's an idea to include that in the loop too using 'i' as a convenient way to name new clips and assign them individual levels.


3) Then you pass each new clip into the star function as a parameter to get the stars drawn.

Here's some code, I've put some squares in there instead of the call to the drawStar function but just take out all the 'lineTo' stuff and put the function call back in when you have it:

Code:
function star(clip) {
	//use 'with' so that you don't have to write 'clip' eash time
	with (clip) {
		clear();
		lineStyle(2, 0x6688AA);
		beginFill(0xFF0000, 100);
		//draw squares instead of stars
		lineTo(50, 0);
		lineTo(50, 50);
		lineTo(0, 50);
                //replace with drawStar
		endFill();
	}
}
//loop code
for (var i = 1; i<=5; i++) {
	clip = this.createEmptyMovieClip('draw_layer'+i, i);
	//position clip
	clip._x = 100*i;
	star(clip);
}
 
that makes a lot more sense. Now one last questions. as I am creating the five stars I want to make it a button so it goes to a different URL for each star. If I use this code

draw_layer1.onRelease = function() {
getURL(codehere)
}

it attaches that URL to draw_button1, but if I put it like this
[draw_layer].onRelease = function() {

then no URL's are added. How do I reference each draw_layer object to add the URL??
 
It kind of messed my message up there. Basically I have an i added to the bracked for draw_layer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top