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

Instancing with a loop 1

Status
Not open for further replies.

strantheman

Programmer
Mar 12, 2001
333
US
I hope this isn't confusing. Im using instances to setup some buttons. In frame one I have:

but_1.onRollOver = function(){
rollover (2);
}

but_2.onRollOver = function(){
rollover (3);
}


Using instances, I can setup all of my responses in one frame for much better code organization than having code in other frames. When you rollover button 1, it executes rollover(2); and when you rollover button 2, it executes rollover(3); ... but I will eventually have about 30 of these buttons. I want to loop over the creation of these instances so I don't have to hand code every one. Here's my for loop:

for (i=0;i<2;i++)
{
incr = i+1;
xxx = &quot;but_&quot; + i + &quot;.onRollOver = function(){ rollover (&quot; + incr + &quot;); }&quot;;
evaluate(xxx);


}

So, it creates a string that sets up the instance and looks exactly like the hand coded one, and then theoretically it would evaluate the string and action script would understand it. I figured the evaluate() command would do this since its description in the Help library is &quot;Action; creates a new empty line and inserts a semicolon (;) for writing statements to be evaluated in the Actions panel. &quot;

I really hope someone out there has done evaluations/ delayed evaluations like this before, and also has knowledge of instancing, a very new MX feature.

thanks for your time :)

strantheman

 
Assuming your buttons' intances on stage are named &quot;but_1&quot; through &quot;but_5&quot;...
Code:
for (i = 1; i < 6; ++i){
    trace(this[&quot;but_&quot; + i]);
    this[&quot;but_&quot; + i].num  = i+1;
    this[&quot;but_&quot; + i].onRollOver = function(){
    trace(&quot;YO!&quot;+this.num);
    };
}
stop();

Regards,

cubalibre2.gif
 
oldnewbie YOU ROCK man

Ok, im using that for loop for onRollOver, onRollOut, and onRelease. The onRelease part is not doing what i'd expect. For some reason it's passing 5 for i for all 4 buttons, instead of 1, 2, 3, 4 for each respective button. Everything else is working great.

The error im getting when I test and click is: Error opening URL &quot;file:///mydirectories/portfolio_5.jpg&quot;

Here's the complete code. Checkout the //releases section.

function showPic (fileNumber, button){
var fileName = &quot;portfolio_&quot; + fileNumber + &quot;.jpg&quot;;
picFrame_mc.picTarget_mc.loadMovie(fileName);
button._alpha = 40;
}

function rollover (frameNum){
thumbs_mc.gotoAndStop(frameNum);
}

function rollout (){
thumbs_mc.gotoAndStop(1);
}

for (i = 1; i <= 4; ++i){
this[&quot;but_&quot; + i].incr = i+1;
// rollovers
this[&quot;but_&quot; + i].onRollOver = function()
{
rollover(this.incr);
};

// rollouts
this[&quot;but_&quot; + i].onRollOut = function()
{
rollout;
}

// releases
this[&quot;but_&quot; + i].onRelease = function()
{
showPic (i, this[&quot;but_&quot; + i]);
}
}

Thanks again man.

strantheman
 
Nevermind, kept staring blankly at the screen till a new idea popped into my head. I just made a variable like you did with this[&quot;but_&quot; + i].incr = i+1; Instead I just set one = i;

Thanks a lot oldnoob. You rock man. Star coming your way

strantheman
 
Ok here's another. :p

In my showPic() function I change the alpha of the button clicked to 40.

function showPic (fileNumber, button){
var fileName = &quot;portfolio_&quot; + fileNumber + &quot;.jpg&quot;;
picFrame_mc.picTarget_mc.loadMovie(fileName);
button._alpha = 40;
}

This works:

showPic (this.tempi, but_1);

but this does not

showPic (this.tempi, this[&quot;but_&quot; + i]);

What do you think?

strantheman



 
Think I'd have to see your .fla.
Also think I'm gonna hit the sack!
Mom's birthday tomorrow, so won't be around much!



Regards,

cubalibre2.gif
 
Fixed it, the answer was showPic (this.tempi, this);

see ya

strantheman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top