PDA

View Full Version : re-usable code


rt30000
03-27-2003, 02:28 PM
Hello. This is probably a simple question. I have a script I use a lot within buttons. I'd like to call the script so I don't have to keep using it over and over. It seems i cannot use a function becuase it has mouse events or something. Any help? Here is the script within the button:

________________

on (rollOver) {
tellTarget ("_level0.projectname") {
gotoAndStop (_level0.project);
}
}
on (release) {
tellTarget ("_level0.projectdescriptions") {
gotoAndStop (_level0.project);
}
}
on (rollOut) {
tellTarget ("_level0.projectname") {
gotoAndStop ("none");
}
}
________________

I'd like to use this for all buttons. How can I include it? Thank you very much in advance!

Robert Truex

catbert303
03-27-2003, 02:45 PM
If you give the buttons instance names myButton1, myButton2 etc you can then dynamically assign the code to all of them in a loop,

on the main timeline


for (var i = 0; i < N; ++i) { // where N is 1 higher than the number used to name your last button, eg if you have buttons myButton1 and myButton2 N = 3
_root["myButton" + i].onRollOver = function() {
_level0.projectname.gotoAndStop(_level0.project);
}
_root["myButton" + i].onRelease = function() {
_level0.projectdescriptions.gotoAndStop(_level0.pr oject);
}
_root["myButton" + i].onRollOut = function() {
_level0.projectname.gotoAndStop("none");
}
}

catbert303
03-27-2003, 02:49 PM
alternatively paste your code into notepad (or any other text editor) and save it as buttons.as (in the same place as your fla file), then on each button use the action

#include "buttons.as"

be careful not to add a semi colon to the end of that line though (it will cause an error)

rt30000
03-27-2003, 02:54 PM
Ok, so I need to exchange "N" for the number of buttons I am using (plus 1)?

And this may sound stupid (I'm sure it does) but how to you give a button an instance name?:confused:

thanks a bunch!
RobT

rt30000
03-27-2003, 02:56 PM
I knew about the "include" way (though I have never used it) but that simply places that code everywhere it is called throughout the movie when it is published. I am trying to cut down on the amount of code in the movie. THANKs again!

simontheak
03-27-2003, 02:59 PM
Originally posted by rt30000
how to you give a button an instance name?
RobT

Click on the button and then have a look on the Properties bar. At the top on the left there should be a little text field. That's the one you want - you can type in your instance names in there

catbert303
03-27-2003, 02:59 PM
but if you use include and you need to change the code fror some reason it only needs changing in one place. :)

Assuming you are using flash MX you give a button an instance name by selecting the button filling in the instance name field in the properties panel

rt30000
03-27-2003, 03:02 PM
I am using Flash 5. I know where you fill in instance names, but when its a button it does not give you the option. Is there a way to hand code it in"expert mode"?

simontheak
03-27-2003, 03:04 PM
Ah ... problem there then. I think I'm right in saying that you can't give buttons instance names in Flash 5

catbert303
03-27-2003, 03:09 PM
in flash 5 buttons can't have instance names, you could do something like this though,


// main timeline
function buttonRollOver() {
_level0.projectname.gotoAndStop(_level0.project);
}
function buttonRelease() {
_level0.projectdescriptions.gotoAndStop(_level0.pr oject);
}
function buttonRollOut() {
_level0.projectname.gotoAndStop("none");
}


then on each button add the code,


on (rollOver) {
_root.buttonRollOver();
}
on (release) {
_root.buttonRelease();
}
on (rollOut) {
_root.buttonRollOut();
}

rt30000
03-27-2003, 03:11 PM
good call, I think that will be the answer! Thanks a million guys!