Skip to main content
Participating Frequently
November 5, 2010
Answered

unified Rollover function on multiple MC's

  • November 5, 2010
  • 1 reply
  • 923 views

Hi all,

I'm trying to code a variable rollover for a sequence of MCs on the stage.

I have 3 buttons (MovieClips) on the stage and each time one is rolled over I'm making a different movieclip appear.

So I've done this in a root timeline frame script:

//put an empty container movieclip on the stage

containerMC = _root.createEmptyMovieClip("container", this.getNextHighestDepth());

//button 1  - "but1" is my linked instance name and Content 1 is my content movieclip

but1.onRollOver = function() {
trace("You rolled over on the movie clip '"+this._name+"'.");
newContent=_root.attachMovie("Content1","container",containerMC.getDepth());
newContent._x=300;
newContent._y=200;

};

and so on for each button (1, 2 and 3).

As you can imagine if there were 20 buttons there is a lot of uneccessary duplication.

What I've considered is, as they are sequential I should be able to read "but"+i and "content"+i and I could push the button names and content names into an array.  The thing I'm having trouble with is, how do I write the function so it checks which clip is rolled over and then attaches the corresponding content movieclip?

Ultimately I would like the script to check how many buttons there are on the stage and simply iterate for that number (so I don't have to specify how many there are to loop through i.e. numOfButtons = 4),  But not sure how to go about it.

As you can see the code could be the same for each button, if I could read the movieclip rolled over and attach the function variably.

In summary what I'm trying to do is have a single function for all rollover buttons named but1 to but n.

Any guidance would be appreciated.

Chris

This topic has been closed for replies.
Correct answer kglad

Hi kglad,

Ok, have tried it again and double checked all linkages but still only works no more than twice.  Will use trace and see what I can deduce.

Thankyou again or your help.

Regards

Chris


i have an error in removeF().  that should be:

function removeF() {
    for (obj in containerMC) {
        if (typeof (containerMC[obj]) == "movieclip") {
            containerMC[obj].removeMovieClip();
        }
    }
}

1 reply

kglad
Community Expert
Community Expert
November 5, 2010

unless you can make some assumptions (that are probably unwise), you need to specify the number of movieclip buttons:


//put an empty container movieclip on the stage

containerMC = _root.createEmptyMovieClip("container", this.getNextHighestDepth());

for(var i:Number=1;i<=buttonNumber;i++){  // define buttonNumber

this["but"+i].ivar=i;

this["but"+i].onRollOver=function(){

removeF();

var mc:MovieClip=containerMC.attachMovie("Content"+this.ivar,"content"+this.ivar,containerMC.getNextHighestDepth());

mc._x=300;

mc._y=200;

}

}

function removeF(){

for(obj in containerMC){

if(typeof(containerMC[obj])=="movieclip"){

containerMC.removeMovieClip(containerMC[obj]);

}

}

}


Participating Frequently
November 5, 2010

Thanks Kglad,

I've inserted the script and set buttonNumber to 3 prior to entering into the for loop.

Unfortunately it only works twice and then halts - irrespective of the number of buttonNumber.

Would I have to wrap it in an enterFrame.

So there's no way to see how many movieclips are on the stage at any one time called "but"+i?

If there was I could use that to set the iteration, but wouldn't have a clue how to do it.

Thanks for your help, it's certianly well further along that I was.

Chris