Skip to main content
May 1, 2014
Answered

class 102-- create a class with 3 different function

  • May 1, 2014
  • 3 replies
  • 406 views

For example I have 300 buttons!

I have to define 3 function(ROLL_OVER) for every 100 buttons.

I mean three Different ROLL_OVER in one class.

I am confused!

I know how to do this in  main fla but not in external .as file!

for example

//btn type 1

var type1:array=[x1,x2,x3,x4];

for(var i:uint=0;i<type1.length;i++)

{

type1.addEventListener(MouseEvent.ROLL_OVER, over1)

function over1(e:mouseEvent):void

          {

trace("type 1 rolled over");

          }

}

//btn type2

var type2:array=[y1,y2,y3,y4];

for(var i:uint=0;i<type2.length;i++)

{

type2.addEventListener(MouseEvent.ROLL_OVER, over2)

function over2(e:mouseEvent):void

          {

trace("type 2 rolled over");

          }

}

To be more exact, I want to know can i create 'rollover' and 'rollout' and 'click' function for all of my buttons in one single class ? or I should create a class for every type ?

This topic has been closed for replies.
Correct answer kglad

correct.

so what?  your constructor only needs to add the 3 listeners.

3 replies

Inspiring
May 1, 2014

It would be a performance killer to add an event listener to each button.  It seems that you have 3 different types of buttons, and you want to assign a different handler for each type of button.

You could handle this with a single listener.

Mouse events bubble to the parent DisplayObjectContainer, and their parents, etc.  So you could just add a single listener to the highest common parent of the buttons (i.e. their "shared container"). The "target" property of the event will tell you which button was clicked, and can simply check which type of button was clicked to forward the event to the proper handler (e.g. over1, over2, or over3).  Make sure the mouseChildren property of each of your buttons is set to false, so that child objects inside each button cannot become the target, and only the button itself will be mouse event target.

shared_container.addEventListener( MouseEvent.ROLL_OVER, rollOverHandler );

function rollOverHandler( e:MouseEvent ):void

{

     var clickedButton:Object = e.target; //target is object that received mouse event

     if (clickedButton is ButtonClassType1)

          over1( e ); //forward event to handler for button type 1

     else if (clickedButton is ButtonClassType2)

          over2( e ); //forward event to handler for button type 2

     else if (clickedButton is ButtonClassType3)

          over3( e ); //forward event to handler for button type 3

}

Amy Blankenship
Legend
May 1, 2014

If you set up your button as a SimpleButton (by setting its symbol type to Button), you don't need MOUSE_OVER handlers.

kglad
Community Expert
Community Expert
May 1, 2014

you could create one button class that contain 3 different rollovers and make your buttons class members.

May 1, 2014

but as far as I got , every class can only have one constructor (function)..

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
May 1, 2014

correct.

so what?  your constructor only needs to add the 3 listeners.