views:

28

answers:

2

I am working on a component where i need to register a click event on a button inside the moviclip which is inside a tilelist, can somebody point me in the right direction.

Tilelist -> movieclip -> button (i need to register a click event on the button).

A: 

To simulate a mouse click event from any displayObject you can do something like this:

targetMC.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
TandemAdam
+1  A: 

If I understand you correctly, it seems all you want is a handle to the movieclip inside your tileList and from there, you want to add an event listener.

There are many ways to get a handle to the movieClip inside your tileList. First, I'd say you should be making use of a dataProvider along the lines of the example here. This would serve as the model for your display object. From there, you can just access your desired movieclip from within the list as in:

import fl.controls.TileList;
import fl.data.DataProvider;

var dp:DataProvider = new DataProvider();
dp.addItem(moviClip1);
dp.addItem(moviClip2);
dp.addItem(moviClip3);
dp.addItem(moviClip4);

var myTileList:TileList = new TileList();
myTileList.dataProvider = dp;
addChild(myTileList);

...

//sometime later in the code
//this adds a listener to movieClip3
dp.getItemAt(3).addEventListener(MouseEvent.CLICK, onClick);

...

//or
for (var mc in dp.toArray()) {
    (mc as DisplayObject).addEventListener(MouseEvent.CLICK, onClick);
}

Another approach (if you can't work with the data provider) would be to try your luck with the following TileList methods:

getChildAt(index:int):DisplayObject
getChildByName(name:String):DisplayObject
getChildIndex(child:DisplayObject):int  
getItemAt(index:uint):Object

and see if you can get access to the clip you want. If it were me, I'd test by doing something like:

var mc:MovieClip = myTileList.getChildAt(1) as MovieClip;
if(mc != null) mc.addEventListener(MouseEvent.CLICK, doSomethingWhenClicked);

public void function doSomethingWhenClicked(event:MouseEvent):void {
    Alert.show('click!', 'click');
}

Then, I'd keep trying different things on line 1 until the alert popped up.

I hope that helps in some way,

--gMale

EDIT: In response to your comment below, here is a quick example off the top of my head of you might play around with your clip and find the button (it may not compile but you should get the idea). Use this only if you don't have an ability to edit the clip and if its button is actually a fl.controls.Button (as opposed to a MovieClip button):

import fl.controls.Button;

var buttonsFound:Integer = 0;

//assume mytiledClip points to your clip that has the button on it
for(var i=0; i < myTiledClip.numChildren; i++) {
    var button:Button = myTiledClip.getChildAt(i) as Button;
    if(button != null){
        Alert.show('Button Found at index:' + i, 'Button Found');
        buttonsFound++;
    }
}
Alert.show('Number of buttons found: ' + buttonsFound, 'Search Complete');   
gmale
Thanks for the answer, i am able to register a click event for teh movie clip what i need is, there is a child (button) inside the movie clip, i need to register a click even on the button.
pmarreddy
if you have control over the movie clip, then edit it so the item you need has an instance name. Then you can reference it easily as in *myClip.myButton.addEventListener(MouseEvent.CLICK, onClick)*. If you don't have control of the clip, you can use it's list of children to get access to the button as in *myClip.getChildAt(0)*. You can play around and figure out which child is your button. I'll write some example code in my response.
gmale