all groups > flash actionscript > august 2006 >
You're in the

flash actionscript

group:

Is there a better way to do this?


Re: Is there a better way to do this? tralfaz
8/10/2006 3:22:41 PM
flash actionscript:

[quoted text, click to view]

If you want to assign button actions in a loop you can use numbered buttons like btn1, btn2 etc. or you can use an array of button
names in the loop.
Unless you are planning to make a lot more things happen with the buttons it doesn't seem worth all the trouble. If you just want
to make some common rollover and rollout actions you can make a function that all buttons can use by sending their instance names to
the function like this..

// buttons
//-----------------------------------
calendar.onRollOver = function() {
rollOverStuff(this);
}
//-----------------------------------
calendar.onRollOut = function() {
rollOutStuff(this);
}
//-----------------------------------

// common functions
//-----------------------------------
function rollOverStuff(btn)
{
btn._alpha = 20;
btn._y = 55; // button drops down a little
}

//-----------------------------------
function rollOutStuff(btn)
{
btn._alpha = 100;
btn._y = 50; // button back up
}

//-----------------------------------

tralfaz

Is there a better way to do this? Josh Pratt
8/10/2006 9:24:16 PM
I've got a menu I'm programing, but I am starting to forsee it being a lot of
code.. Is there a way to cut down this code specifically? I've got it so that
the alpha level of a button goes down to 60 onRollOver and the alpha goes back
to 100 onRollOut. Is there a better way to do this? Here is my code:

calendar.onRollOver = function() {
calendar._alpha = 60;
}
calendar.onRollOut = function() {
calendar._alpha = 100;
}
programs.onRollOver = function() {
programs._alpha = 60;
}
programs.onRollOut = function() {
programs._alpha = 100;
}
media.onRollOver = function() {
media._alpha = 60;
}
media.onRollOut = function() {
media._alpha = 100;
}
bios.onRollOver = function() {
bios._alpha = 60;
}
bios.onRollOut = function() {
bios._alpha = 100;
}
serving.onRollOver = function() {
serving._alpha = 60;
}
serving.onRollOut = function() {
serving._alpha = 100;
}
Re: Is there a better way to do this? Rothrock
8/10/2006 10:33:37 PM
The answer is yes. However you might want to give up your nice easy names and
do something like this:

for(var i=0;i<5;i++){
this["myButton"+i].onRollover=function(){
this._alpha=60;
}
this["myButton"+i].onRollOut=function(){
this._alpha=100;
}
}

In that case your buttons would need to be named myButton0, myButton1, and so
on. You could even make this a bit snazzier by creating an array to hold the
names of your buttons, the label that will show, different colors, etc.
whatever you wanted. Then step through the array and apply the values. There
are lots of ways to do this, experiment and play around.
Re: Is there a better way to do this? Josh Pratt
8/11/2006 12:31:03 AM
Thanks for the reply. I'm just going to go ahead and assume that it works great. I won't be able to try it until work tommorow.

Thanks again,

Re: Is there a better way to do this? Josh Pratt
8/11/2006 3:08:21 PM
Rothrock,

Re: Is there a better way to do this? Rothrock
8/11/2006 5:46:49 PM
Just an idea to get you started down a road. I'm typing this up off the top of
my head so there may be a typo or two. You might want to try it out on a brand
new empty file before you try and jam it into your current project. Or just
keep it as an idea for the next one.

I'm guessing you're making some kind of menu or such. Right?

Personally I don't use buttons so for this to work you will need to make a
movie clip with whatever "button art you want" and a textfield called myLabel.
Put that movieclip into the library and set its linkage to buttonArt.

First try it with only the three things in the array. Then try adding another
and see how that works for ya!

sections=new Array();
sections[0]={label: "Calendar", desc: "Check out our calendar"}
sections[1]={label: "Programs", desc: "Our current programs"}
sections[2]={label: "Bios", desc: "Get to know us a bit."}

//the spacing and placement of the buttons
var xLoc=100;
var yLoc=20;
var ySpace=40;

for(var i=0;i<sections.length;i++){
var curClip = this.attachMovie("myLabel","button"+i,1000+i)
curClip.myID=i;
curClip.myLabel.text=sections[i].label;
curClip._x=xLoc;
curClip._y=yLoc+ySpace*i;
curClip.onRollOver=function(){
this._alpha=60;
trace(sections[this.myID].desc)
}
curClip.onRollOut=function(){
this._alpha=100;
}
}
AddThis Social Bookmark Button