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

forked from: removeChild don't works if adding code

This is a simple rectangle drawing tool
when you comment off the part begins
case Keyboard.ESCAPE:
removeChild don't work. why this is so?
you can removeChild rectangle by pressing ENTER

マウスで四角形を描くだけのものです
以下の部分から始まるコメントを外すと
case Keyboard.ESCAPE:
removeChildが動かなくなるのですが、何故なのでしょうか?
Enterキーを押すと長方形をremoveChildします
tested under
FlashDevelop3.3 + fsch Version 4.1.0 build 16076
breakが抜けていました。
ここらへんを参考にしてください。
http://livedocs.adobe.com/flex/3_jp/langref/statements.html#switch
Get Adobe Flash player
by shohei909 18 Oct 2010
    Embed
/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ldgU
 */

// forked from hogefugabar's removeChild don't works if adding code
//This is a simple rectangle drawing tool
//when you comment off the part begins
//case Keyboard.ESCAPE:
//removeChild don't work. why this is so?
//you can removeChild rectangle by pressing ENTER
//
//マウスで四角形を描くだけのものです
//以下の部分から始まるコメントを外すと
//case Keyboard.ESCAPE:
//removeChildが動かなくなるのですが、何故なのでしょうか?
//Enterキーを押すと長方形をremoveChildします

//tested under
//FlashDevelop3.3 + fsch Version 4.1.0 build 16076


//breakが抜けていました。
//ここらへんを参考にしてください。
//http://livedocs.adobe.com/flex/3_jp/langref/statements.html#switch

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.*;
    import flash.ui.*;
    import flash.utils.ByteArray;
    public class FlashTest extends Sprite {
        private var mouse_b:Boolean;
        private var redoHistory:Array;
        private var color:uint;
        private var cx:int;
        private var cy:int;
        private var current:Sprite;
        public function FlashTest() {
            mouse_b = false;
            color = 0xff0000;
            redoHistory = new Array();
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onmouse);
            stage.addEventListener(MouseEvent.MOUSE_UP, onmouseup);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onmousemove);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onkey);
        }
        private function onkey(event:KeyboardEvent):void {
            switch(event.keyCode) {
                case Keyboard.ENTER:
                    trace("before numChildren="  + numChildren);
                    if (this.numChildren >= 1) {
                        redoHistory.push(removeChildAt(numChildren - 1))
                    }
                    trace("after numChildren="  + numChildren);
                    for (var q:uint = 0; q < numChildren;++q) {
                        trace("childs[" + q + "]=" + getChildAt(q));
                    }
                    trace(""); 
                    
                    //breakがないとEnterを押したときにcase Keyboard.ESCAPEも実行されてしまいます。
                    break;
                case Keyboard.ESCAPE:
                    if (redoHistory.length >= 1) {
                        addChild(redoHistory.pop())
                    }
                    break;
            }
        }
        private function onmouse(event:MouseEvent):void{
            mouse_b = true;
            current = new Sprite();
            cx = mouseX;
            cy = mouseY;
            addChild(current);
        }
        private function onmouseup(event:MouseEvent):void {
            current.graphics.beginFill(color);
            current.graphics.drawRect(cx, cy, mouseX - cx, mouseY - cy);
            current.graphics.endFill();
            mouse_b = false;
        }
        private function onmousemove(event:MouseEvent):void{
            if (!mouse_b) { return; }
            current.graphics.clear();
            current.graphics.beginFill(color);
            current.graphics.drawRect(cx, cy, mouseX - cx, mouseY - cy);
            current.graphics.endFill();
        }
    }
}