Actionscripting Question

I have created an animated rollover effect that consists of a button that is motion tweened inside of a movie clip with the instance name, “rollover”. Also inside this movie clip is a dynamic text box with the variable ‘text’.

I would like to use this one movie clip multiple times, but i would like to code it so that when the button is clicked, it uses information from AS that is in the root directory. For example: the button would have something like:

on (press) {
if instance name is rollover1, then getURL(“URLone”, _blank");
if instance name is rollover2, then getURL(“URLtwo”, _blank");
else getURL(“URLthree”,_blank");
}

and the AS in the main would contain something like this:

URLone=“http://www.urlone.com”;
URLtwo=“http://www.urltwo.com”;
URLthree=“http://www.urlthree.com”;
rollover1.text = “Button One Text”;
rollover2.text = “Button Two Text”;
rollover3.text = “Button Three Text”;

Each button would have an instance name of rollover1, rollover2, and so on…

The idea is to be able to use the same button rollover clip for all the buttons, but have a different name show on the button, and have a different URL to go to. I know it can be done, im just not sure how.

Since I can’t think of how to explain it in English right now check my attached file with commented coding.

[edit]
Please note that there are multiple ways of getting this to work. You can use movie clips as buttons to make it easier, but since your animating it still needs to be in a clip.

And you don’t need to place the actions on the button itself. You can give the button an instance name and target it in the for loop via an onPress dynamic event handler.
[/edit]

thats pretty much what i wanted, although it seems a little complicated. Am i to put the URLs in the ‘var goTo’ line such as this?

var goTo = [“http://www.aol.com”, “http://www.nbc.com”, “http://www.cbs.com”, “http://www.abc.com”, “http://www.kirupa.com”];

BTW: My goal was to write an Animated Rollover Buttons Tutorial, but i didn’t want it to be too complicated.

Thanks for your help.

Well reusing clips isn’t always easy :wink:

Animated rollOvers reusing the same clips as buttons isn’t exactly as easy as just making buttons. You don’t need to use a for loop to create the clips if you don’t want, you can instead drag multiple instances of the clip onto the stage at once, give them all an instance name and target them that way.

And yes, you populated that array correctly.

I modified the original file as attached. I removed the for loop and dragged instances of the clip onto the stage manually and gave them instance names.

I also wrote the arrays in a different manner. There are many ways to write arrays (4 if I remember correctly) so this is just another method if you think it is easier.

Its more coding, but if you think it is easier and it still works, then what the heck right?

With that last file as an example since you aren’t use a for loop you don’t need to use arrays and can shorten your coding this way…

::check attachment::

sweet! this seems to be exactly what i needed.

Once again, LIB saves the day!

Cool, glad I could help :slight_smile:

I have a habit of overdoing my coding sometimes, so don’t mind me…lol. I think that last file is more along the lines of what you actually needed. I can optimize that even more with prototypes probably…hmmm. Im bored, looking for something to do, have an idea… I think I will try it.

well, since you mention it…

why doesn’t a new window open when the button is clicked?

is it set up to work like getURL?

can it also be used to load a swf dynamically, like with loadMovie?

Since I love dynamic event handlers I used that method instead. It keeps all the coding neccesary in one area and makes it easier to edit and allows you to do whatever you want for each button.

:check attachment:

:shouts outloud, like an herbal essence commercial:: “yes yes yes!!”

this is awesome… i wanted to have multiple buttons, with the same rollover effect, but without having to create six different buttons with six different commands. This allows me to recycle the same button clip, change a few instance names, and add all the details in the actionscripting! I love it!

If i do manage to write a tutorial, you can be sure that you will get the props that you deserve!!

thanks a lot! :cool:

LIB: On friday, i had a working version on my desktop, and now it is gone. Now i can’t get the code to work with the rollover effects, and its frustrating the hell out of me.

Your FLA’s work fine, but they stop working after i make the button rollover animated. Attached is the FLA that i am working with, and im wondering if you can take a look at it (when you get a chance) to tell me where i am going wrong. Thanks.

My guess is that when i add the on(rollOver) commands to control the button rollover animation, it somehow conflicts with the onPress AS. I’ve experimented, but with no luck.

Well I don’t know the exact problem, it may because of the tween, or because of the onPress command, I don’t have much time to figure this out right now so heres a quick workaround…

Remove all of this… [AS]button1.clickMe.onPress = function() {
getURL(“http://www.cbperformance.com”, “_blank”);
};
button2.clickMe.onPress = function() {
getURL(“URL2”, “_self”);
};
button3.clickMe.onPress = function() {
getURL(“URL3”, “_top”);
};
button4.clickMe.onPress = function() {
getURL(“URL4”, “_blank”);
};
button5.clickMe.onPress = function() {
getURL(“URL5”, “_self”);
};[/AS]

And add the on (press) {} commands to the buttons themselves as you did with the rollOver and rollOut.

its cool if you are busy, dont sweat it.

That workaround defeats the purpose, which is having the commands in the AS. The Flash files you supplied work well, but they cease to work once i create the rollovers. The thing that sucks is that i had a working version on Friday, and when i came back on Tuesday, it was no longer there. My computer is set up so that a lot of my files are stored on a server, including my desktop, and the My Documents folder, and screwy stuff is always happening. The same three folders that i delete everyday, will magically reappear every morning. Its kinda frustrating, but i’ve learned to live with it.

thanks for your help.

Well after attempting something real quick I came up with this…

Remove all actions from the button in the clip.

Add a stop action to frame 8.

Update the actions on the frame with this…
[AS]button1.nameLabel.text = “CB Performance”;
button2.nameLabel.text = “you want”;
button3.nameLabel.text = “this”;
button4.nameLabel.text = “thing”;
button5.nameLabel.text = “to say”;
Button.prototype.rollFade = function() {
this.onRollOver = function() {
this._parent.gotoAndPlay(2);
};
this.onRollOut = function() {
this._parent.gotoAndPlay(9);
};
};
//using dynamic event handlers we can remove the on(press) command from the button itself
button1.clickMe.rollFade();
button1.clickMe.onPress = function() {
getURL(“http://www.cbperformance.com”, “_blank”);
};
button2.clickMe.rollFade();
button2.clickMe.onPress = function() {
getURL(“URL2”, “_self”);
};
button3.clickMe.rollFade();
button3.clickMe.onPress = function() {
getURL(“URL3”, “_top”);
};
button4.clickMe.rollFade();
button4.clickMe.onPress = function() {
getURL(“URL4”, “_blank”);
};
button5.clickMe.rollFade();
button5.clickMe.onPress = function() {
getURL(“URL5”, “_self”);
};[/AS]

The thing is this though, when you press down you have the hit state of the button turn white. This only works between frames 2-7 and 9-14. The only frame it doesn’t work on is frame 8. I really don’t know why though, but frame 8 is the frame we stop on with the rollOver, so I don’t get it. I am guess it might be the tween that is causing this problem, but I don’t know for sure.

Thank you, lost! this finally works!!

I’m pretty sure that the hit state doesn’t show in frame eight because I have tint set at 100%. As a matter of fact, i changed it to 50%, and now you can see the button turn white when it is pressed. Again, thanks a lot for helping me with this.

Wow, you were right, it was the tint. I was racking my brain trying to figure out what was up lol.

Well at least everything finally works :slight_smile:

I am attaching the file here just in case your comp goes all ballistic retard on you again.

haha. thanks. i was about to upload it myself to share with people, but i think i’ll just attempt to turn it into a tutorial.

Do you think there would be demand for a “Dynamic Rollover Menu” tutorial?

Also, would you mind if i used your code for the tutorial? You would of course get credit for your contributions…

<object classid=“clsid:D27CDB6E-AE6D-11cf-96B8-444553540000” codebase=“http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0” width=“200” height=“200”>
<param name=“movie” value=“http://www.igovw.com/rollover.swf”>
<param name=“quality” value=“high”>
<param name=“menu” value=“false”>
<param name=“wmode” value=“transparent”>
<embed src=“http://www.igovw.com/rollover.swf” menu=“false” quality=“high” pluginspage=“http://www.macromedia.com/go/getflashplayer” type=“application/x-shockwave-flash” width=“200” height=“200”></embed></object>

A demand? Hmm, maybe. I know I use dynamic menus a lot, they come in handy big time, so perhaps there could be a demand for it.

And you can use my code all you want. Open source :wink:

just a shorter way to do it (i guess) :slight_smile:

captionsArray = ["CB Performance", "you want", "this", "thing", "to say"];
urls = ["http://www.cbperformance.com", "URL2", "URL3", "URL4", "URL5"];
Button.prototype.rollFade = function() {
	this.onRollOver = function() {
		this._parent.gotoAndPlay(2);
	};
	this.onRollOut = function() {
		this._parent.gotoAndPlay(9);
	};
};
for (i=1; i<6; i++) {
	this["button"+i].nameLabel.text = captionsArray[i-1];
	this["button"+i].clickMe.rollFade();
	this["button"+i].clickMe.onPress = function() {
		getURL(urls[i-1], "_blank");
	};
}

Thats sorta like what I did the first time ahmed.

But the thing is, people might not want to name their buttons in succession like that and instead have individual names.

And that would involve the creation of another array and blah blah blah…lol.

And also seperate onPresses allow for not only getURLs but for loadMovies or getURLs or attachMovies or whatever they want for each button, and it isn’t limited to just one thing.