Tutorial :add an event to the movieclip inside tilelist in as3


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).


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,


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');     


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

targetMC.dispatchEvent(new MouseEvent(MouseEvent.CLICK));  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »