topical media & game development

talk show tell print

#graphic-flex-image-effects-03-Flex-SetPixelTest.ax

#graphic-flex-image-effects-03-Flex-SetPixelTest.ax [swf] [flash] flex


  package {
  
          import flash.display.Bitmap;
          import flash.display.BitmapData;
          import flash.display.Sprite;
          import flash.events.MouseEvent;
  
          [SWF(width=600, height=400, backgroundColor=0x000000)]
  
          
Simple drawing application using setPixel32() to color 5 pixels around the mouse position.

  
          public class @ax-graphic-flex-image-effects-03-Flex-SetPixelTest extends Sprite {
  
                  private var _canvas:BitmapData;
  
                  
Constructor. This creates the bitmap data that will be drawn into and sets up listeners for when the mouse is pressed and released on the stage.

  
                  public function @ax-graphic-flex-image-effects-03-Flex-SetPixelTest() {
                          _canvas = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
                          var bitmap:Bitmap = new Bitmap(_canvas);
                          addChild(bitmap);
                          stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
                          stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
                  }
  
                  
Handler for when the mouse is depressed on the stage. This sets up a listener for when the mouse moves.
parameter: event Event dispatched by stage.

  
                  private function onStageMouseDown(event:MouseEvent):void {
                          stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                  }
  
                  
Handler for when the mouse is released on the stage. This removes the listener for when the mouse moves.
parameter: event Event dispatched by stage.

  
                  private function onStageMouseUp(event:MouseEvent):void {
                          stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                  }
  
                  
Handler for when the mouse is moved about the stage. This colors five pixels on or around the mouse position.
parameter: event Event dispatched by stage.

  
                  private function onStageMouseMove(event:MouseEvent):void {
                          var x:Number = event.localX;
                          var y:Number = event.localY;
                          // how far out from the mouse position the four periphery points are colored
                          var radius:uint = 10;
                          // don't update the bitmap until unlock is called
                          _canvas.lock();
                          _canvas.setPixel32(x, y, 0xFFFFFFFF);
                          _canvas.setPixel32(x+radius, y, 0xFFFF0000);
                          _canvas.setPixel32(x-radius, y, 0xFF00FF00);
                          _canvas.setPixel32(x, y+radius, 0xFF0000FF);
                          _canvas.setPixel32(x, y-radius, 0xFFFF00FF);
                          _canvas.unlock();
                          event.updateAfterEvent();
                  }
  
          }
  
  }


(C) Æliens 04/09/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.