Flash / Flex / ActionScript/Graphics/Rectangle — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 09:19, 26 мая 2010

A rectangle in a Sprite is nested within another Sprite. Both Sprite instances are rotated 45 degrees.

 

package {
    import flash.display.GradientType;
    import flash.display.Sprite;
    import flash.geom.Matrix;
    import flash.geom.ColorTransform;
    public class Main extends Sprite {
    
         public function Main() {
    
            var rect1:Sprite = new Sprite(  );
            rect1.graphics.lineStyle(1);
            rect1.graphics.beginFill(0x0000FF, 1);
            rect1.graphics.drawRect(0, 0, 75, 50);
            
            var rect2:Sprite = new Sprite(  );
            rect2.graphics.lineStyle(1);
            rect2.graphics.beginFill(0xFF0000, 1);
            rect2.graphics.drawRect(0, 0, 75, 50);
            rect2.x = 50;
            rect2.y = 75;
            
            // Create the container
            var group:Sprite = new Sprite(  );
            
            // Add the rectangles to the container
            group.addChild(rect1);
            group.addChild(rect2);
            
            // Add the container to the main application
    
            
            group.x = 40;
            group.scaleY = .15;
            group.rotation = 15;
    
            var innerGroup:Sprite = new Sprite(  );
            
            innerGroup.addChild(group);
            innerGroup.rotation = 15;
            addChild(innerGroup);
         }
    
    }
}



Draw a rectangle

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        try {
          drawRectangle(this, 100, 200);
          drawRectangle(this, 300, 400);
        }
        catch(errObject:Error) {
          this.graphics.clear(  );
          trace("An error occurred: " + errObject.message);
        }
    }
    private function drawRectangle(sprite:Sprite, nWidth:Number, nHeight:Number):void {
      if(isNaN(nWidth) || isNaN(nHeight)) {
        throw new Error("Invalid dimensions specified.");
      }
      sprite.graphics.lineStyle(1, 0, 1);
      sprite.graphics.lineTo(nWidth, 0);
      sprite.graphics.lineTo(nWidth, nHeight);
      sprite.graphics.lineTo(0, nHeight);
      sprite.graphics.lineTo(0, 0);
    }
  }
}



Draw Colored Rectangle

 
package {
  import flash.display.Sprite;
  import flash.display.Shape;
  import flash.display.Graphics;
  
  public class Rectangles extends Sprite {
    
    public function Rectangles() {
      
      drawColoredRectIn(graphics, 0xFF0000);
      
      var rect:Shape = new Shape();
      drawColoredRectIn(rect.graphics, 0xFFFF00);
      rect.x = 50;
      rect.y = 50;
      addChild(rect);
    }
    
    private function drawColoredRectIn(target:Graphics, color:int):void {
      target.lineStyle(1, 0x000000);
      target.beginFill(color);
      target.drawRect(0, 0, 100, 100);
    }
  }
}



Fill a rectangle

 
package{
  import flash.display.*;
  import flash.filters.*;
  public class Main extends Sprite{
    public function Main(){
        var box:Sprite = new Sprite(  );
        box.graphics.lineStyle(  );
        box.graphics.beginFill(0xFFFFFF);
        box.graphics.drawRect(0, 0, 100, 100);
        box.graphics.endFill(  );
        addChild(box);
        box.filters = [new DropShadowFilter(10), new GlowFilter(  )];
    }
  }
}



rotateChildren( ), applies the generalized code from Example 20-4. It randomly rotates all the descendants of a specified container (not just the children).

 
package {
  import flash.display.*;
  import flash.events.*;
  public class RotatingRectangles extends Sprite {
    public function RotatingRectangles (  ) {
      var rects:Array = new Array(  );
      for (var i:int = 0; i < 20; i++) {
        rects[i] = new Shape(  );
        rects[i].graphics.lineStyle(1);
        rects[i].graphics.beginFill(Math.floor(Math.random(  )*0xFFFFFF), 1);
        rects[i].graphics.drawRect(0, 0, 100, 50);
        rects[i].x = Math.floor(Math.random(  )*500);
        rects[i].y = Math.floor(Math.random(  )*400);
        addChild(rects[i]);
      }
      stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
    }
    private function mouseDownListener (e:Event):void {
      for (var i:int=0; i < numChildren; i++) {
        getChildAt(i).rotation = Math.floor(Math.random(  )*360);
      }
    }
    public function rotateChildren (container:DisplayObjectContainer):void {
      for (var i:int = 0; i < container.numChildren; i++) {
        var thisChild:DisplayObject = container.getChildAt(i);
        if (thisChild is DisplayObjectContainer) {
          rotateChildren(DisplayObjectContainer(thisChild));
        } else {
          thisChild.rotation =  Math.floor(Math.random(  )*360);
        }
      }
    }
  }
}



Rotating Rectangles

 
package {
  import flash.display.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main (  ) {
      var rects:Array = new Array(  );
      for (var i:int = 0; i < 20; i++) {
        rects[i] = new Shape(  );
        rects[i].graphics.lineStyle(1);
        rects[i].graphics.beginFill(Math.floor(Math.random(  )*0xFFFFFF), 1);
        rects[i].graphics.drawRect(0, 0, 100, 50);
        rects[i].x = Math.floor(Math.random(  )*500);
        rects[i].y = Math.floor(Math.random(  )*400);
        addChild(rects[i]);
      }
      stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
    }
    private function mouseDownListener (e:Event):void {
      for (var i:int=0; i < numChildren; i++) {
        getChildAt(i).rotation = Math.floor(Math.random(  )*360);
      }
    }
  }
}



Square

 
package {
     
     import flash.display.Sprite;
     
     [SWF(width=550, height=400)]
     
     public class Main extends Sprite {  
     
          public function Main() {
               var square:Square = new Square();
               addChild(square);
               
               // Move to (300,300)
               square.x = 30;
               square.y = 30;
               
               // Stretch horizontally and squash vertically
               square.scaleX = 2;
               square.scaleY = 0.5;
               
               // Make 50% alpha
               square.alpha = 0.5;
               
               // Rotate 45 degrees
               square.rotation = 45;
          }
     
     }
     
}
class Square extends flash.display.Sprite {
     public function Square() {
          graphics.lineStyle(5);
          graphics.beginFill(0xFF);
          graphics.drawRect(0, 0, 100, 100);
          graphics.endFill();
     }
}