topical media & game development

talk show tell print

sample-flex-draw.mx

sample-flex-draw.mx [swf] flex


  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
  
      <mx:Script>
          <![CDATA[
  
              import mx.containers.Canvas;
  
              private var x1:uint;
              private var y1:uint;
              private var x2:uint;
              private var y2:uint;
  
              private var isDrawing:Boolean = false;
      
  
              private function mouseDown(evt:MouseEvent):void {
                  isDrawing = true;
                  x1 = evt.localX;
                  y1 = evt.localY;
                  box.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
                  box.graphics.moveTo(x1, y1);
                  box.graphics.beginFill(0xff0000);
  
              }
              
              private function mouseMove(evt:MouseEvent):void {
                  
                  
                  
                  box.graphics.moveTo(evt.localX, evt.localY);
  
              }
              
              
  
              private function mouseUp(evt:MouseEvent):void {
                  box.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
                  box.graphics.endFill(); 
                  isDrawing = false;
                  x2 = evt.localX;
                  y2 = evt.localY;
  
                  var w:uint = x2 - x1;
                  var h:uint = y2 - y1;
  /*
                  var can:Box = evt.currentTarget as Box;
                  can.graphics.beginFill(0xFF0000, 0.4);
                  can.graphics.drawRect(x1, y1, w, h);
                  can.graphics.endFill();
  */
                  trace(x1, y1, x2, y2);
              }
          ]]>
      </mx:Script>
  
      <mx:Canvas id="canvas"
              backgroundColor="white"
              width="100%"
              height="100%">
          <mx:Box id="box"
                  width="100%"
                  height="100%"
                  mouseDown="mouseDown(event);"
                  mouseUp="mouseUp(event);" />
      </mx:Canvas>
  
  </mx:Application>
  
  


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.