Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

class 102-- create a class with 3 different function

Guest
May 01, 2014 May 01, 2014

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 ?

TOPICS
ActionScript
340
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 01, 2014 May 01, 2014

correct.

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

Translate
Community Expert ,
May 01, 2014 May 01, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 01, 2014 May 01, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 01, 2014 May 01, 2014

correct.

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 01, 2014 May 01, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 01, 2014 May 01, 2014
LATEST

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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines