pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

ActionScript pastebin - collaborative debugging tool View Help


Posted by ShortWave on Mon 20 Oct 13:31 (modification of post by view diff)
report abuse | download | new post

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
  3.         <mx:Script>
  4.         <![CDATA[
  5.                
  6.             import mx.controls.Alert;
  7.                         // public var xBitmap:Bitmap;
  8.                         private var bitmapData:BitmapData;
  9.                         private var searchColorA:uint = 0xFFFFFF;
  10.                         private var searchColorB:uint = 0x000000;
  11.                        
  12.             private function init():void {
  13.                 var camera:Camera = Camera.getCamera();
  14.                 camera.setMode( 640, 480, 25, false );
  15.                
  16.                 if (camera) {
  17.                     video.attachCamera(camera);
  18.                 } else {
  19.                     Alert.show("You don't seem to have a camera.");
  20.                 }
  21.                
  22.                 this.addEventListener(Event.ENTER_FRAME, handleActivity );
  23.                 // camera.addEventListener(ActivityEvent.ACTIVITY, handleActivity );
  24.                 var g:Graphics = red.graphics;
  25.                 g.lineStyle(1, 0xFF0000, 0.75 );
  26.                 g.beginFill(0xFF0000, 0.75 );
  27.                 g.drawCircle(0, 0, 10);
  28.                 g.endFill();
  29.                
  30.                 g = green.graphics;
  31.                 g.lineStyle(1, 0x00FF00, 0.75);
  32.                 g.beginFill(0x00FF00, 0.75);
  33.                 g.drawCircle(0, 0, 10);
  34.                 g.endFill();
  35.             }
  36.            
  37.             private function askPermission( ) : void {
  38.                 // Security.showSettings( SecurityPanel.PRIVACY );
  39.             }
  40.                        
  41.                         private function handleActivity( event:Event ) : void {
  42.                                 if (bitmapData == null) {                                      
  43.                                         bitmapData = new BitmapData( video.width, video.height );
  44.                                         // bData.draw( video );
  45.                                         // xBitmap = new Bitmap( bData );
  46.                                         // target.source = xBitmap;
  47.                                 }
  48.                                
  49.                                 bitmapData.draw( video );
  50.                                
  51.                                 // now that we have some nice data in the bitmapData property, let's proceed
  52.                                
  53.                                 var matchedA_x:Array = new Array();
  54.                                 var matchedA_y:Array = new Array();
  55.                                
  56.                                 var matchedB_x:Array = new Array();
  57.                                 var matchedB_y:Array = new Array();
  58.                                
  59.                                
  60.                                
  61.                                 for (var y:int = 0; y < bitmapData.height; y++) {
  62.                                         for (var x:int = 0; x < bitmapData.width; x++) {
  63.                                                 if (match(searchColorA, bitmapData.getPixel(x, y) )) {
  64.                                                         matchedA_x.push( x );
  65.                                                         matchedA_y.push( y);
  66.                                                 } else if (match( searchColorB, bitmapData.getPixel( x, y ))) {
  67.                                                         matchedB_x.push( x );
  68.                                                         matchedB_y.push( y );
  69.                                                 }
  70.  
  71.                                         }
  72.                                 }
  73.                                
  74.                                 var a_avg_x:int = 0;
  75.                                 var a_avg_y:int = 0;
  76.                                 var b_avg_x:int = 0;
  77.                                 var b_avg_y:int = 0;
  78.                                 var i:int;
  79.                                 if (matchedA_x.length > 0) {
  80.                                        
  81.                                         for (i = 0; i < matchedA_x.length; i++ ) {
  82.                                                 a_avg_x = a_avg_x + matchedA_x[i];
  83.                                                 a_avg_y = a_avg_y + matchedA_y[i];
  84.                                         }
  85.                                         a_avg_x = a_avg_x / matchedA_x.length;
  86.                                         a_avg_y = a_avg_y / matchedA_y.length;
  87.  
  88.                                  }
  89.                                  if (matchedB_x.length > 0) {
  90.                                         for (i = 0; i < matchedB_x.length; i++) {
  91.                                                 b_avg_x = b_avg_x + matchedB_x[i];
  92.                                                 b_avg_y = b_avg_y + matchedB_y[i];
  93.                                         }
  94.                                         b_avg_x = b_avg_x / matchedB_x.length;
  95.                                         b_avg_y = b_avg_y / matchedB_x.length;
  96.                                  }
  97.                                  
  98.                                  // move the canvases
  99.                                  red.x = a_avg_x;
  100.                                  red.y = a_avg_y;
  101.                                  green.x = b_avg_x;
  102.                                  green.y = b_avg_y;
  103.                                
  104.                         }
  105.                         private function setColor( event:MouseEvent ) : void {
  106.                                 var x:int = event.localX;
  107.                                 var y:int = event.localY;
  108.                                 searchColorA = bitmapData.getPixel( x, y );
  109.                         }
  110.                        
  111.                         private function setOtherColor( event:MouseEvent ) : void {
  112.                                 var x:int = event.localX;
  113.                                 var y:int = event.localY;
  114.                                 searchColorB = bitmapData.getPixel( x, y );
  115.                         }
  116.                        
  117.                        
  118.                         private function match( colorA:uint, colorB:uint ) : Boolean {
  119.                                 var limit:int = 20;
  120.                                 var sr:int = colorA >> 16 & 0xFF;
  121.                                 var sg:int = colorA >> 8 & 0xFF;
  122.                                 var sb:int = colorA & 0xFF;
  123.                                
  124.                                 var cr:int = colorB >> 16 & 0xFF;
  125.                                 var cg:int = colorB >> 8 & 0xFF;
  126.                                 var cb:int = colorB & 0xFF;
  127.                                 return cr > sr - limit && cr < sr + limit && cg > sg - limit && cg < sg + limit && cb > sb - limit && cb < sb + limit;
  128.                         }
  129.                        
  130.         ]]>
  131.     </mx:Script>
  132.         <mx:Canvas>
  133.         <mx:VideoDisplay id="video" height="480" width="640" creationComplete="askPermission()" rightClick="setOtherColor( event )" click="setColor( event )"/>
  134.                 <!-- <mx:Image id="target" height="640" width="480"/> -->
  135.                 <mx:Canvas id="red"/>
  136.                 <mx:Canvas id="green"/>
  137.         </mx:Canvas>
  138.  
  139.     <mx:Button id="button"
  140.             label="Attach Camera"
  141.             click="init();" />
  142. </mx:WindowedApplication>

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post