code_junkie

How to create custom MouseEvent.CLICK event in AS3 (pass parameters to function)?

Nov 13th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. for(var i:Number; i < 10; i++){
  2. var someVar = i;
  3. myClips[i].addEventListener(MouseEvent.CLICK, doSomething);
  4. }
  5.  
  6. function doSomething(e:MouseEvent){ /* */ }
  7.  
  8. for(var i:Number; i < 10; i++){
  9.  
  10. var someVar = i;
  11. myClips[i].addEventListener(MouseEvent.CLICK, function(){
  12. doSomething(someVar);
  13. });
  14. }
  15.  
  16. function doSomething(index){ trace(index); }
  17.  
  18. public class MyClip
  19. {
  20. public var myPublicProperty:int;
  21.  
  22. public function MyClip() { //... }
  23. }
  24.  
  25. for (var i:int = 0; i < 10; i++)
  26. {
  27. myClips[i].myPublicProperty = i;
  28. myClips[i].addEventListener(MouseEvent.CLICK, doSomething);
  29. }
  30.  
  31. function doSomething(event:MouseEvent):void
  32. {
  33. trace(event.currentTarget.myPublicProperty.toString());
  34. }
  35.  
  36. import flash.events.Event;
  37.  
  38. //custom event class to enable the passing of strings along with the actual event
  39. public class TestEvent extends Event
  40. {
  41. public static const TYPE1 :String = "type1";
  42. public static const TYPE2 :String = "type2";
  43. public static const TYPE3 :String = "type3";
  44.  
  45. public var parameterText:String;
  46.  
  47. public function TestEvent (type:String, searchText:String)
  48. {
  49. this.parameterText = searchText;
  50. super(type);
  51. }
  52.  
  53. }
  54.  
  55. dispatchEvent(new TestEvent(TestEvent.TYPE1, 'thisIsTheParameterText'))" ;
  56.  
  57. someComponent.addEventListener(TestEvent.TYPE1, listenerFunction, true , 0, true);
  58.  
  59. for (var i=0; i<5; i++) {
  60. myClips[i].addEventListener( MouseEvent.CLICK, getHandler(i) );
  61. }
  62.  
  63. function getHandler(i) {
  64. return function( e:MouseEvent ) {
  65. test(i);
  66. }
  67. }
  68.  
  69. function test( j ) {
  70. trace("foo "+j);
  71. }
  72.  
  73. private function myCallbackFunction(e:Event, parameter:String):void
  74. {
  75. //voila, here's your parameter
  76. }
  77.  
  78. private function addArguments(method:Function, additionalArguments:Array):Function
  79. {
  80. return function(event:Event):void {method.apply(null, [event].concat(additionalArguments));}
  81. }
  82.  
  83. var parameter:String = "A sentence I want to pass along";
  84. movieClip.addEventListener(Event.COMPLETE, addArguments(myCallbackFunction, [parameter] ) );
  85.  
  86. var view:Object=[];
  87.  
  88. for(var i:uint=0;i<Camera.names.length;i++){
  89.  
  90. view[i]=getChildByName("Cam"+i);
  91.  
  92. //_________ Add Text _________
  93. var tf:TextField = new TextField();
  94. tf.autoSize = TextFieldAutoSize.LEFT;
  95. tf.textColor=0xffffff;
  96. view[i].tf=view[i].addChild(tf);
  97.  
  98. //_________ Add Timer _________
  99. var t:Timer = new Timer(1000);
  100. view[i].timer=t;
  101. view[i].timer.start();
  102. view[i].timer.addEventListener(TimerEvent.TIMER, addArg(i));
  103. }
  104.  
  105. function addArg(adi:uint):Function {
  106. return function(event:TimerEvent):void {
  107. updatecamstatus(adi);
  108. }
  109. }
  110. function updatecamstatus(vH:uint):void {
  111. with (view[vH]){
  112. tf.text="Cam" + vH + "n";
  113. tf.appendText("activityLevel: " + videosource.activityLevel + "n");
  114. tf.appendText("motionLevel: " + videosource.motionLevel + "n");
  115. }
  116. }
Add Comment
Please, Sign In to add comment