all groups > flash actionscript > september 2005 >
You're in the

flash actionscript

group:

Using single function for mutliple buttons



Using single function for mutliple buttons posted.by(Vern);
9/21/2005 10:06:29 PM
flash actionscript: This may be hard to explain, please stick with me ...

I want to have an underline slide into postion under the button that has
been clicked. Easy, done it plenty of time. I want to do it more
efficiently, using AS. (I use MX2004Pro)

So I am trying to figure a way to:

when any given button is clicked, capture its name, its position and its
width, all using the same function. So I would get a different value for
each of six buttons for each property, name, x position and width - all from
the SAME function every time. I am confident this is fairly easy, but I
can't quite get it. I keep getting NaN and undefined.

Can anyone give me some direction on that?

Thanks!
--
Regards,
--Vern
===========================
onClipEvent(doSomethingStupid){
setProperty("Face", color, #FF0000);
_root.audio = "uh oh!";
}

Re: Using single function for mutliple buttons posted.by(Vern);
9/21/2005 10:22:34 PM
Well, I have tried so many things. I thought maybe trying to use an array
somehow might do the trick. Not much of a coder, I usually stumble through
these things until I hit the right combo. This is a bit more difficult.
Here is what I had been working on when I decided to post, haven't even
tried this, 'cause I know it's not good! - don't laugh too hard at me!!
:(

var myButtons = new Array();
button[0] = _root.button1;
button[1] = _root.button2;
button[2] = _root.button3;
button[3] = _root.button4;
button[4] = _root.button5;
button[5] = _root.button6;

button[i].onpress = capClick();

function capClick(){
butNum = this.button[i];
trace(butNum);
}

I went ahead and tried this, and it does nothing. I can see that there is
no way to capture any button click here. Am I still going to have a
myButton.onpress = someFunction(); for each button? Maybe I am trying to
do the impossible and that is keeping from getting the rest of it right.

Anyway, THANKS!

--
--
Regards,
--Vern
===========================
onClipEvent(doSomethingStupid){
setProperty("Face", color, #FF0000);
_root.audio = "uh oh!";
}

Re: Using single function for mutliple buttons posted.by(Vern);
9/21/2005 11:33:36 PM
OK. I am doing my best to analyze and understand this. First, I appreciate
your efforts in sharing this with me.

Now then, I am using a single image from photoshop that includes the
button labels, so I have no text to measure the width of. Instead, I have
placed buttons over each, adjust the length, and made them invisble (named
*button1* through *button6*). I have tested and can indeed read the width
and postion of them this way, and they will function. So I will have to
modify this part; *var totalWidth =
referenceClip.menuButton_mc.label_txt._width - 5* I think I can figure that
out.

What I can't seem to grasp is how the script knows what button has been
pressed, and therefor how it gets any properties. I see the *menuButton_mc*
but don't understand how each of the buttons is differenciated from the
others. Apparrently the variable *currentlySelected* contains this info,
but how? Also, the third line turns off visibility of the entire movie.
Here is what I have: One frame. four layers. Layer one is AS only. Layer
two contains the invisible buttons. Layer three is the underline_mc, and
layer four is the background image.

Thanks so much for your time!

--
--
Regards,
--Vern
===========================
onClipEvent(doSomethingStupid){
setProperty("Face", color, #FF0000);
_root.audio = "uh oh!";
}

Re: Using single function for mutliple buttons posted.by(Vern);
9/21/2005 11:53:07 PM
No need for appologies! I REALLY appreciate your help! I am going to go to
work on this in the morning, it is about midnight here now. Will post and
let you know how I am doing with it tomorrow.

Thanks again!

--
--
Regards,
--Vern
===========================
onClipEvent(doSomethingStupid){
setProperty("Face", color, #FF0000);
_root.audio = "uh oh!";
}

Re: Using single function for mutliple buttons mandingo
9/22/2005 12:00:00 AM
well there are a number of errors in your code...

things like :

var myButtons = new Array(); // defining an array as myButtons
button[0] = _root.button1 // then using a different name when assigning
values to it

your button[x].onpress = capClick();
is only going to work if there is a loop that defines the presses as they
relate to the array elements...

ok... let's step away from that...

I had an underline on a previous site... all it really took was a bit of
planning... and nothing needed to be done on the button for the underline...

this.onEnterFrame = function(){
if(this._parent.currentlySelected == null){
this._visible = false;
return;
}
this._visible = true;
if(this.isTweening() != true){ // if I am not already moving
// some code needs to go here to move to the currently selected item
var referenceClip = this._parent.currentlySelected;
// want to know where this is in reference to the actual text it is
underlining
var offset = referenceClip._x + referenceClip.menuButton_mc.bullet_mc._width
+ 13;
// need to know the width of the text it is underlining to resize line
var totalWidth = referenceClip.menuButton_mc.label_txt._width - 5;
this._width = totalWidth;
// slide the nav line along in a 1 sec interval
this.slideTo(offset,this._y,1);
}
}

the only thing the buttons then did was set a variable on the parent timeline

this._parent.currentlySelected = this;

the nav underline did the rest...

cheers
Re: Using single function for mutliple buttons NSurveyor
9/22/2005 12:00:00 AM
You could pass the button itself into the function... ie:

function moveLine(thisBtn){
trace(thisBtn._name);
trace(thisBtn._x);
trace(thisBtn._width);
//other stuff;
}

later on....

someBtn.onPress = function(){
moveLine(this);
}

You might wanna post the code so we can help with what you ard doing wrong...
Re: Using single function for mutliple buttons mandingo
9/22/2005 12:00:00 AM
sorry... I didn't consider that I hadn't fully explained it...

movieClip instance of underline has that code attached to it... that way the
onEnterFrame is relating only to the underline... (that will stop your whole
movieClip from disappearing too :) )

modify it however you need...

now on the main timeline add this :

this.currentlySelected = null;

that will start off to indicate to the underline that nothing is selected so
it will stay switched off...

then on your button instances (and you can do this in a loop)

for(var k=1; k<7;k++){
this["button"+k].onRelease = function(){
this._parent.currentlySelected = this;
}
}

that should get you started... sets the onRelease for each of the buttons to
change the currentlySelected variable... then the underline does the rest...
hopefully

cheers


Re: Using single function for mutliple buttons albee
9/22/2005 6:30:06 AM
Rather than wrestling all of this code, maybe an example would help? http://www.cristancho.net/button_behaviors/buttonBehaviors.zip. I'm using Flash MX 2004...

Good luck,

Re: Using single function for mutliple buttons posted.by(Vern);
9/23/2005 2:47:22 PM
Thanks NSurveyor, mandingo and albee. Each of you helped my understand a
bit more about how to achieve this type of thing. Although I arrived at a
different solution, I was able to use something from each of you and I very
much appreciate your time and effort in helping me in this regard.

I have never been a programer, but you guys obviously are or at least have a
good grasp on OOP and such things. So another question, can you guys point
me in the direction of a/some tutorials that can help me get a handle on
this stuff? Specifically, NSurveyor mention passing an a value to a
function. That lost me. I am aware that it can be done, but I don't have a
grasp on what it means or the mechanics of it. Any direction would be
great!

Thanks again for you time and efforts in helping me! All three of you were
invaluable in helping to a solution!

--
--
Regards,
--Vern
===========================
onClipEvent(doSomethingStupid){
setProperty("Face", color, #FF0000);
_root.audio = "uh oh!";
}

Re: Using single function for mutliple buttons NSurveyor
9/23/2005 8:56:06 PM
Look up Function in the Help Panel. Basically, if you have a function:

function traceME(){
trace("YOU TRACED ME!");
}
traceME();

when you execute that function, it will run this code. But what if you wanted
to tell the traceME function, to trace whatever you wanted, without creating a
new function for each trace? Well, you can use arguments and parameters:

Ever noticed the () after a function? Sure, you know this calls the function,
but this is also the place to put the variables you want to give to the
function:

function traceME(traceText){
trace(traceText);
}
traceMe("HELLO!!!!");
traceMe("GoodBYE!!!!");

Notice, the traceText in the parenthesis... this is a variable (called an
argument) that is given to the function when you call it. Notice in the calls
of the function, you have "HELLO!!!!" between the parenthesis, this is a
variable (called a parameter). This value is passed to the function as the
traceText variable. Then, inside the function, you can do whatever you like
with that variable.

But, what if you wanted to pass two parameters to the function?

function traceME(traceText,otherText){
trace("1. "+traceText);
trace("2. "+otherText);
}
traceMe("HELLO!!!!","GoodBYE!!!!");

You just put commas between each variable you want to pass both in the
functions parenthesis and the function call's parenthesis.

Hope that is understandable?
Re: Using single function for mutliple buttons posted.by(Vern);
9/23/2005 11:46:00 PM
Yeah, that make more sense than any of that has before. So how would I then
capture a button as the variable in a function? If I could do that, then I
could deterimine any available property of that button, right? I am getting
the concept here anyway, about passing variables through a function call. I
have always known there is a lot I am missing there, and the few times I
tried to get a handle on it, it may have well be Chineese! So THANKS! I
love breaking new ground!

--
--
Regards,
--Vern
===========================
onClipEvent(doSomethingStupid){
setProperty("Face", color, #FF0000);
_root.audio = "uh oh!";
}

Re: Using single function for mutliple buttons mandingo
9/23/2005 11:46:22 PM
AS1.0 doesn't really adhere to the whole true OOP architecture... in fact it
lets you do pretty much whatever you want...

that being said, reference books and tutorials won't really clearly define
what you are after because it wasn't really a consideration at the time.

I have spent the last few days re-reading an AS2.0 book (Colin Moock's -
Essential Actionscript 2.0) where he explains in depth how and why you maintain
as much OOP principles as possible in your coding. (For cosmetic
maintainability, for more robust componentry and for easier interaction with
your end client).

Along the way he gives hints that will actually improve performance also.

Advice would be get on board with AS2.0 (Flash MX04 or Studio 8) and grab this
book.

By the way, I am not a programmer either (or at least I had never been before
I started on Flash a couple of years ago and have never been "taught" other
than what I have discovered on this forum) and it wasn't a hard read for this
book.

cheers
AddThis Social Bookmark Button