topical media & game development

talk show tell print

#graphic-flex-image-effects-01-Flex-DrawingStraightLines.ax

#graphic-flex-image-effects-01-Flex-DrawingStraightLines.ax [swf] [flash] flex


  package {
  
          import flash.display.Shape;
          import flash.display.Sprite;
          import flash.events.MouseEvent;
          import flash.geom.Point;
  
          [SWF(width=550, height=400, backgroundColor=0xFFFFFF)]
  
          
Demonstrates how to draw straight lines with the drawing API.

  
          public class @ax-graphic-flex-image-effects-01-Flex-DrawingStraightLines extends Sprite {
  
                  private var _currentShape:Shape;
                  private var _color:uint;
                  private var _startPosition:Point;
  
                  
Constructor. Sets up listeners for mouse events.

  
                  public function @ax-graphic-flex-image-effects-01-Flex-DrawingStraightLines() {
                          stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
                          stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
                  }
  
                  
Draws a line from the start position to the current mouse position.

  
                  private function drawLine():void {
                          _currentShape.graphics.clear();
                          _currentShape.graphics.lineStyle(3, _color);
                          _currentShape.graphics.moveTo(_startPosition.x, _startPosition.y);
                          _currentShape.graphics.lineTo(stage.mouseX, stage.mouseY);
                  }
  
                  
Handler for when the stage is clicked. This creates a new shape for the current line, saves the starting posiiton and sets up a listener for when the mouse moves.
parameter: event Event dispatched by the stage.

  
                  private function onStageMouseDown(event:MouseEvent):void {
                          _color = Math.random()*0xFFFFFF;
                          _currentShape = new Shape();
                          addChild(_currentShape);
                          _startPosition = new Point(stage.mouseX, stage.mouseY);
                          stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                  }
  
                  
Handler for when the mouse is released. This removes the listener for when the mouse moves.
parameter: event Event dispatched by the stage.

  
                  private function onStageMouseUp(event:MouseEvent):void {
                          stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                  }
  
                  
Handler for when the mouse moves. This updates the drawn line based on the current mouse position.
parameter: event Event dipsatched by the stage.

  
                  private function onStageMouseMove(event:MouseEvent):void {
                          drawLine();
                          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.