2010年4月24日 星期六

ActionScript3 Sprite 特性

Sprite的特性


  • SpriteWidthHeight屬性是來自於繼承鍊中的抽象父類別DisplayObject
  • DisplayObject只是個抽象的顯示物件(他不能實體化)他的Width與Height是用來形容任何繼承他的顯示物件的長寬。
  • Sprite是一個容器,其容器的長、寬是由子顯示物件的長寬來決定。(容器是拿來裝東西的,所以其長寬當然是看裝的東西的位置與長寬來決定)。


下例Sprite的Width直接給予數值會改變其Scalx來因應改變......
  200 x = 300 ;  x = 300 / 200

Example:
package
{
   import flash.display.DisplayObject;
   import flash.display.DisplayObjectContainer;
   import flash.display.GradientType;
   import flash.display.SpreadMethod;
   import flash.display.Sprite;
   import flash.utils.Dictionary;

   [SWF(width=1024 , height=600 )]
   public class SpriteTest extends Sprite
   {
     private var parentsContainer:Sprite;
     private var child1:DisplayObject;
     private var child2:DisplayObject;
     
     //建構子
     public function SpriteTest()
     {
            init();
            initEnvironment();
     }
     private function init():void
     {
            parentsContainer = new Sprite();
            parentsContainer.x=0;
            parentsContainer.y=0;
            this.addChild(parentsContainer);
     }
     private function initEnvironment():void
     {
       //主要測試對像,在未加入Child實的數值
       reportProperty(parentsContainer);
       child1 = createRectangle();
       parentsContainer.addChild(child1);

       //加入chlid1後的數值,注意長寬
       reportProperty(parentsContainer);

       var child2X:Number = child1.x + child1.width;

       child2 = createRectangle(0x009900,child2X);
       parentsContainer.addChild(child2);

       //加入chlid2後的數值
       reportProperty(parentsContainer);

       parentsContainer.width = 300;
        
       //注意scaleX

       reportProperty(parentsContainer);
       parentsContainer.width = 200;
       reportProperty(parentsContainer);
       child2.x+=200;
       reportProperty(parentsContainer);
    }

    public function createRectangle(color:uint=0xFC1471, x:Number=0 , y:Number=0 ,
                                                    width:Number=100 ,  height:Number=100 ):DisplayObject
  {
       var rectangle:Sprite = new Sprite();
             rectangle.graphics.beginFill(color);
             rectangle.graphics.drawRect( x , y , width , height);
             rectangle.graphics.endFill();
       return rectangle;
  }

    public function reportProperty(target:DisplayObject):void
    {
        trace("↓");
        trace("x = " + target.x);
        trace("y = " + target.y);
        trace("scaleX = " + target.scaleX);
        trace("scaleY = " + target.scaleY);
        trace("width = "  + target.width);
        trace("height = " + target.height);
        trace("↑");
    }
  }
}

輸出結果:


x = 0
y = 0
scaleX = 1
scaleY = 1
width = 0
height = 0



x = 0
y = 0
scaleX = 1
scaleY = 1
width = 100
height = 100



x = 0
y = 0
scaleX = 1
scaleY = 1
width = 200
height = 100



x = 0
y = 0
scaleX = 1.5
scaleY = 1
width = 300
height = 100



x = 0
y = 0
scaleX = 1
scaleY = 1
width = 200
height = 100


x = 0
y = 0
scaleX = 1
scaleY = 1
width = 400
height = 100

沒有留言:

張貼留言