# Problem button.rollOver code in for loop

**URL:** https://forum.kirupa.com/t/problem-button-rollover-code-in-for-loop/46327
**Category:** flash
**Created:** [March 23, 2004, 12:53pm UTC](https://forum.kirupa.com/t/problem-button-rollover-code-in-for-loop/46327 "2004-03-23T12:53:43Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Mosley](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Mosley](https://forum.kirupa.com/u/Mosley)
#### Post date: [March 23, 2004, 12:53pm UTC](https://forum.kirupa.com/t/problem-button-rollover-code-in-for-loop/46327/1 "2004-03-23T12:53:43Z")

</div>

I have three movieclips (used as buttons) on my main MC. The following code swaps the depth of a dynamically loaded image the when I rollOver each movieclip (button).

I want to put the button.rollOver code in a for loop so I dont have to repeat this code for every image loaded.

[AS]  
x = 150; // x location of image  
Y = 100; // y location of image  
onLoad = function () {  
\_global.nextDepth = 1;  
\_global.displayed = 0;  
};  
// Import photos  
for (i=1; i\<8; i++) {  
this.createEmptyMovieClip(“clip”+i+"\_mc", i);  
this[“clip”+i+"\_mc"].createEmptyMovieClip(“photo\_mc”, 0);  
this[“clip”+i+"\_mc"][“photo\_mc”].loadMovie(“pics/pic”+i+".jpg");  
// put the movieclip at x znd y locations  
this[“clip”+i+"\_mc"].\_x = x;  
this[“clip”+i+"\_mc"].\_y = y;  
// scale photos  
this[“clip”+i+"\_mc"][“photo\_mc”].\_xscale = 100;  
this[“clip”+i+"\_mc"][“photo\_mc”].\_yscale = 100;

button1.onRollOver = function() {  
if (displayed != 1) {  
nextDepth = \_root.getNextHighestDepth();  
clip1\_mc.swapDepths(nextDepth);  
displayed = 1;  
}  
}  
button2.onRollOver = function() {  
if (displayed != 2) {  
nextDepth = \_root.getNextHighestDepth();  
clip2\_mc.swapDepths(nextDepth);  
displayed = 2;  
}  
}  
button3.onRollOver = function() {  
if (displayed != 3) {  
nextDepth = \_root.getNextHighestDepth();  
clip3\_mc.swapDepths(nextDepth);  
displayed = 3;  
}  
}  
}  
[/AS]
