In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

自分自身をremoveChild

Get Adobe Flash player
by mooney_STR 09 Sep 2009
/**
 * Copyright mooney_STR ( http://wonderfl.net/user/mooney_STR )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/72Ag
 */

// forked from mooney_STR's spread_triangle
package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.Event;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            var FW:Number = this.stage.stageWidth;
            var FH:Number = this.stage.stageHeight;
            var box_size:Number = 46.5;
            var xNum:int = FW / (box_size / 2);
            var yNum:int = FH / box_size;
            var objNum:int = xNum * yNum;
            var tX:Number = 0;
            var tY:Number = 0;
            var xCount:int = 0;
            var yCount:int = 0;
            var i:int;
            for ( i=0 ; i < objNum ; i++ ) {
                if(xCount >= xNum) {
                    xCount = 0;
                    yCount++;
                }
                tX = xCount * box_size;
                tY = yCount * box_size;
                var spr:Sprite = makeCircle(box_size,tX,tY);
                addChild(spr);
                spr.addEventListener(MouseEvent.CLICK, clickHandler);
                xCount++;
            }
          
            function makeCircle(box_size:Number,tX:Number,tY:Number):Sprite {
                var box:Sprite = new Sprite();
                var circle:Sprite = new Sprite();
                circle.graphics.beginFill(0xff0000,1);
                circle.graphics.drawCircle(0,0,box_size / 2 - 1);
                box.addChild(circle);
                box.x = tX + box.width / 2;
                box.y = tY + box.height / 2;
                return box;
            }
            function clickHandler(evt:MouseEvent):void {
                evt.target.addEventListener(Event.ENTER_FRAME, efHandler);
            }
            function efHandler(evt:Event):void {
                var tgt:Sprite = Sprite(evt.target);
                tgt.scaleX -= 0.1;
                tgt.scaleY -= 0.1;
                if(tgt.scaleX <= 0) {
                    tgt.removeEventListener(Event.ENTER_FRAME, efHandler);
                    tgt.removeEventListener(MouseEvent.CLICK, clickHandler);
                    
                    
                    //自分自身をremoveChild 3つの方法
                    //方法1(単純)
                    //tgt.parent.removeChild(tgt);
                    
                    //方法2(インデックスで)
                    //var myInd:int = tgt.parent.getChildIndex(tgt);
                    //tgt.parent.removeChildAt(myInd);
                    
                    //方法3(名前で)
                    var myName:String = "tgt"
                    tgt.name = myName;
                    var rC:Sprite = Sprite(tgt.parent.getChildByName(myName))
                    rC.parent.removeChild(rC);
                }
            }
        }
    }
}