0

I have a container_mc, with lots of child_mcs inside. I want the child_mcs to have full button-like behaviors (click-able, cursor effects).

I am not interested in putting individual mouse listeners on every child. ... I would like to simply have one listener on the parent container, though the parent would effectively be inactive ... only the child_mcs.

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
dsdsdsdsd
  • 2,880
  • 6
  • 41
  • 56

4 Answers4

0

Delfine the common behaviour of the buttons in a class and associate that class with the button symbol in the library.

null
  • 5,207
  • 1
  • 19
  • 35
  • but I believe that I would need to put an eventlistener within that mc-class ... and thus every instance of the mc would effectively create a unique instance of the listener ... – dsdsdsdsd May 23 '15 at 14:00
  • What is your motivation behind not to putting a listener on each button? The cursor effects of a button is functionality that belongs to the button. Also, there's no such thing as an "instance of a listener". The event system is just a list of function references to be called. – null May 23 '15 at 14:41
  • if the listener is in the mc-class, then every instance of the mc will register a unique listener ... but it is a good strategy otherwise ... – dsdsdsdsd May 23 '15 at 15:21
0

You could listen to click events on the container with the useCapture flag set to true (addEventListener's third argument) - the listener would be invoked every time any container's child (including grandchildren and so on) is clicked. Then you could check which button was clicked for example by examining its name property.

0

Let's assume you have something like this:

var container_mc:Sprite = new Sprite();
addChild(container_mc);

container_mc.addChild(button1);
container_mc.addChild(button2);
//and so forth with all your buttons

To do what you ask, you would do the following:

//add a click listener to the container
container_mc.addEventListener(MouseEvent.CLICK,click);

//make the buttonMode true on the container, so you get the button hand cursor
container_mc.buttonMode = true;

function click(e:Event):void {
    //e.target is a reference to what was clicked (see caveat after code sample)

    //if all the children of container_mc are of the same custom class, 
       //you could now call a click handler on that item

    MyButtonClass(e.target).myClickHandler(e);

    //or you could use a switch statement
    switch(e.target){
        case button1:
            //button 1 was clicked, do something with it
            break;

        case button2:
            //button 2 was clicked, do something with it
            break;
    }
}

The only caveat here, is an event's target could be any child down line of container. So if button1 had some children and those children had children, the object referenced in e.target could be any of them (which ever was clicked). If you have child object inside your button, then the easiest way to make sure your target is always the button, is do the following:

button1.mouseChildren = false;

That will ensure any mouse events dispatched by the button, will have button1 as the target and not any of it's children.

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
-1

please ignore the downvote ... if your problem is the same as mine, then this solution works

//-- pseudo code --\\

*for the container:*
container.addEventListener( MouseEvent.MOUSE_DOWN , callback_function ) ;
container.buttonMode ...... false
container.mouseEnabled .... false
container.mouseChildren ... true

*for each child:*
child.buttonMode ...... true
child.mouseEnabled .... true
child.mouseChildren ... false
dsdsdsdsd
  • 2,880
  • 6
  • 41
  • 56
  • This makes no sense and is a very poorly constructed answer. It explains nothing and will not be helpful to future visitors. – BadFeelingAboutThis May 30 '15 at 00:06
  • 1
    What I am saying, is that code only answers are generally frowned upon. On top of that, you've posted Pseudo code and the later half of your pseudo code doesn't make much sense (at least to me) and isn't going to solve your problem - though that may just be a typo. At any rate it is a poor answer (regardless of whether it "works" for you or not). Good answers explain code, are well formatted and address the issue rather than just posting some Pseudo code with no explanation. – BadFeelingAboutThis May 31 '15 at 05:06
  • As far as "it only requires one event listener on the container", obviously I know this, as you'll see I answered the question 5 days ago explaining as much to you. – BadFeelingAboutThis May 31 '15 at 05:14