AS3練習,addChild() 與 reparenting @ 邦邦的部落格 :: 隨意窩 Xuite日誌
  • 留言 & 文章索引
    1. 沒有新回應!
  • 流量統計
  • 2000年8月,
    加入e21摩奇創意,開始接觸 Flash 4。

    2004年3月,
    參與MMUG,分享與討論。

    2004年11月,
    通過 VUE 的 Flash MX 2004 Developer 認證。

    2005年6月,
    開始寫 Blog 分享自己所學。

    2005年7月,
    通過 MCI(Macromedia Certified Instractor) - Flash MX 2004 Developer 講師認證。

    2005年12月,
    終於於 DCI 拿到證書,正式成為第一屆也是最後一屆 Macromedia 的認證講師......$%&@#

    2006年8月,
    通過 ACI(Adobe Certified Instractor) - FlashLite 1.1 講師認證。

    2007年3月,
    離開待了六年半的摩奇創意,轉換跑道到BenQ。

    2007年5月,
    http://blog.ben.idv.tw

    2007年9月,
    BenQ品牌代工分家,我們變成新BenQ的母公司佳世達Qisda。

    2008年9月,
    因公司採用技術策略的轉變,故離開待了一年半的Qisda。

    2009年2月,
    加入一家低調的軟體公司,低調的開發著 Flash Game,呵呵~







  • 如何使用RSS
    Powered by Xuite
    2006-12-02 02:57 AS3練習,addChild() 與 reparenting
    平均分數:0 顆星    投票人數:0
    我要評分:

    在 Flash Player 9 中,區分兩個部分,一個是 AVM,負責處理 ActionScript,另一部分是 Rendering Engine,負責顯示畫面。

    所以,當我們 new 一個物件時,只是在 AVM 中新產生了一個物件,並不會自動顯示在場景上,不像以前的 createEmptyMovieClip() 或是 attachMovie() 會直接呈現,現在必須採用 addChild() 或 addChildAt() 才能將 AVM 中建立好的物件顯示在 DisplayObjectContainer 中。

    addChild() 還有另外一種用途,當我們將既有已經加進某個 Container 的項目,再重複使用一次 addChild() 加到其他 Container 時,此 DisplayObject 將會被搬移到新的 Container 中,這種做法叫做:reparenting!

    swf: http://b.mms.blog.xuite.net/b/8/7/0/10067044/blog_36242/dv/9170067/9170067.swf

    完整原始碼如下:

    package
    {
    import flash.display.Sprite;
    import flash.display.SpreadMethod;
    import flash.display.SimpleButton;

    import flash.events.MouseEvent;

    import flash.text.TextField;

    public class TestReparent extends Sprite
    {
    private var container1:Sprite;
    private var container2:Sprite;
    private var container2_1:Sprite;
    private var container2_2:Sprite;

    private var item:Sprite;

    private var sb1:SimpleButton;
    private var sb2:SimpleButton;
    private var sb3:SimpleButton;
    private var sb4:SimpleButton;

    public function TestReparent()
    {
    //建立第一塊容器
    container1 = new Sprite();
    container1.graphics.beginFill(0xff0000, 0.5);
    container1.graphics.drawRoundRect(0, 0, 400, 100, 20, 20);
    container1.graphics.endFill();

    this.addChild(container1);
    container1.name = "container1";
    container1.x = 10;
    container1.y = 10;

    //建立第二塊容器
    container2 = new Sprite();
    container2.graphics.beginFill(0x00ff00, 0.5);
    container2.graphics.drawRoundRect(0, 0, 400, 100, 20, 20);
    container2.graphics.endFill();

    this.addChild(container2);
    container2.name = "container2";
    container2.x = 10;
    container2.y = 120;

    //在第二塊容器中,建立第一塊子容器
    container2_1 = new Sprite();
    container2_1.graphics.beginFill(0xffff00, 0.8);
    container2_1.graphics.drawRoundRect(0, 0, 185, 80, 20, 20);
    container2_1.graphics.endFill();

    container2.addChild(container2_1);
    container2_1.name = "container2_1";
    container2_1.x = 10;
    container2_1.y = 10;

    //在第二塊容器中,建立第二塊子容器
    container2_2 = new Sprite();
    container2_2.graphics.beginFill(0xffff00, 0.8);
    container2_2.graphics.drawRoundRect(0, 0, 185, 80, 20, 20);
    container2_2.graphics.endFill();

    container2.addChild(container2_2);
    container2_2.name = "container2_2";
    container2_2.x = 205;
    container2_2.y = 10;

    //在第一塊容器中,建立一個物體
    item = new Sprite();
    item.graphics.beginFill(0x0000ff, 0.9);
    item.graphics.drawCircle(-10, -10, 20);
    item.graphics.endFill();

    container1.addChild(item);
    item.x = (container1.width) / 2;
    item.y = (container1.height) / 2;

    //按鈕
    sb1 = createNewButton("搬移到容器2");
    sb1.addEventListener(MouseEvent.CLICK, sb1_click);
    this.addChild(sb1);
    sb1.x = 0;
    sb1.y = 0;

    sb2 = createNewButton("搬移到容器2的子容器1");
    sb2.addEventListener(MouseEvent.CLICK, sb2_click);
    this.addChild(sb2);
    sb2.x = 0;
    sb2.y = 30;

    sb3 = createNewButton("搬移到容器2的子容器2");
    sb3.addEventListener(MouseEvent.CLICK, sb3_click);
    this.addChild(sb3);
    sb3.x = 0;
    sb3.y = 60;

    sb4 = createNewButton("回到容器1");
    sb4.addEventListener(MouseEvent.CLICK, sb4_click);
    this.addChild(sb4);
    sb4.x = 0;
    sb4.y = 90;

    }

    private function createNewButton(str:String):SimpleButton
    {
    var txt:TextField = new TextField();
    txt.text = str;
    txt.autoSize = flash.text.TextFieldAutoSize.LEFT;

    var upState:Sprite = new Sprite();
    upState.graphics.beginFill(0xcccccc, 1.0);
    upState.graphics.drawRoundRect(0, 0, txt.width+10, txt.height, 5, 5);
    upState.graphics.endFill();

    upState.addChild(txt);

    return new SimpleButton(upState, upState, upState, upState);
    }

    private function sb1_click(event:MouseEvent):void
    {
    container2.addChild(item);
    item.x = (container2.width) / 2;
    item.y = (container2.height) / 2;
    trace("parent = " + item.parent.name);
    }

    private function sb2_click(event:MouseEvent):void
    {
    container2_1.addChild(item);
    item.x = (container2_1.width) / 2;
    item.y = (container2_1.height) / 2;
    trace("parent = " + item.parent.name);
    }

    private function sb3_click(event:MouseEvent):void
    {
    container2_2.addChild(item);
    item.x = (container2_2.width) / 2;
    item.y = (container2_2.height) / 2;
    trace("parent = " + item.parent.name);
    }

    private function sb4_click(event:MouseEvent):void
    {
    container1.addChild(item);
    item.x = (container1.width) / 2;
    item.y = (container1.height) / 2;
    trace("parent = " + item.parent.name);
    }
    }
    }

    依照順序按下四個按鈕的話,item 會被搬移到不同的容器中,trace 結果如下:

    parent = container2
    parent = container2_1
    parent = container2_2
    parent = container1

     

    邦邦 / Xuite日誌 / 回應(0) / 引用(0) / 好文轉寄
    回應