Posted By

adrianparr on 03/05/10


Tagged

event button Value dictionary as3 store proxy utils joey lott


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Nikolai
tspitzr


AS3 Use Dictionary Obect to Store Values Associated with MovieClips


 / Published in: ActionScript 3
 

To do this sort of thing (ie. passing values to an onRelease event handler) in AS2 I would have used Joey Lott's Proxy class, which was similar to Delegate. To do this in AS3 you could create your own custom class which extends MovieClip and then store the value as a property which can then be retrieved in the event handler, but sometime just using a Dictionary Object is easier and quicker.

  1. // This example assumes there are 10 movieClips on stage named btn1, btn2, btn3 ... etc
  2.  
  3. import flash.utils.Dictionary;
  4.  
  5. var btnDict:Dictionary = new Dictionary();
  6.  
  7. for (var i:uint=1; i<=10; i++) {
  8. var tempBtn:MovieClip = this["btn"+i];
  9. tempBtn.addEventListener(MouseEvent.CLICK, onBtn_CLICK);
  10. tempBtn.buttonMode = true;
  11. tempBtn.mouseChildren = false;
  12. btnDict[tempBtn] = i;
  13. }
  14.  
  15. function onBtn_CLICK(event:MouseEvent):void {
  16. trace(btnDict[event.target]);
  17. }

Report this snippet  

You need to login to post a comment.