[태그:] ActionScript 3.0

  • Color replacement

    I made an effect similar to the paint bucket feature in Photoshop. This replace colors similar to a user defined color with a specific color.

    You can download the BMPFunctions.as file in the following link.

    포토샵의 페인트 버켓 툴과 비슷한 효과를 만들어 보았습니다. 지정된 색과 비슷한 색상을 찾아서 원하는 색상으로 바꿔줍니다.

    클래스 파일은 아래 페이지에서 BMPFunctions.as를 다운로드하면 됩니다.

    PhotoshopのPaint Bucketのような物を作ってみました。選んだ色に似ている色をほかの色に変わります。

    下のリンクのページで BMPFunctions.asファイルをダウンロードできます。

    http://hangunsworld.com/classes/com/hangunsworld/as3/util/

    import com.hangunsworld.as3.util.BMPFunctions;
    
    var bd:BitmapData = new BitmapData(mc.width, mc.height, true, 0×00000000);
    bd.draw(mc);
    
    var bmp:Bitmap = new Bitmap(bd);
    addChild(bmp);
    bmp.x = mc.width;
    
    // Replace Color button event listener
    replace_btn.addEventListener(MouseEvent.CLICK, doReplace);
    function doReplace(evt:MouseEvent):void{
    	var c1:uint = uint(“0x” + c1_txt.text);
    	var c2:uint = uint(“0x” + c2_txt.text);
    	var t:uint = Math.max(0, Math.min(255, uint(t_txt.text)));
    	t_txt.text = t.toString();
    
    	// draws original image
    	bd.draw(mc);
    	// replaces colors
    	bd = BMPFunctions.replaceColor(bd, c1, c2, t);
    }

    Download FLA

  • Using FlashVars in ActionScript 3.0

    You can pass data from an HTML document to a Flash movie via FlashVars or query string.
    In AS2, these data are defined as variables in the main timeline(_root). But you have to access the data using loaderInfo.parameters in AS3.
    HTML문서에서 FlashVars 또는 쿼리 스트링을 사용하여 플래시 무비로 데이터를 전달할 수 있습니다.
    AS2에서는, 이렇게 넘어온 데이터가 메인 타임라인(_root) 상의 변수로 만들어집니다. 그러나 AS3에서는, loaderInfo.parameters를 통하여 접근하게 됩니다.
    FlashVarsおよびquery stringを使ってHTMLドキュメントからFlashム―ビ―にデ―タを伝える事が出来ます。
    AS2では、そのデーターがメーンのタイムライン(_root)の変数に作られます。しかし、AS3ではloaderInfo.parametersを使う事になりました。
    var userID:String = loaderInfo.parameters.userID;
    var pathXML:String = loaderInfo.parameters.pathXML;
  • use “as” to avoid type mis-match error

    for(var i:uint=0; i
    
    This code causes the type mis-match error at compile, because getChildAt() method returns a DisplayObject.
    위 코드는 컴파일 시에 다음과 같은 타입 미스매치 에러를 냅니다. 왜냐하면 getChildAt()메소드는 DisplayObject 객체를 리턴하기 때문이죠.

    1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip.

    To avoid the compile error, you can cast the returned value as MovieClip like this.
    이러한 컴파일 에러를 회피하기 위해 다음과 같이 리턴된 값을 MovieClip로 캐스팅합니다.
    var mc:MovieClip = MovieClip(getChildAt(i));
    You can also use "as".
    또는 "as"를 사용할 수 있습니다.
    var mc:MovieClip = getChildAt(i) as MovieClip;
  • Use “is” instead of “instanceof”

    is와 instanceof 연산자는 주어진 객체가 특정한 데이터 타입인지를 판별하기 위해 사용됩니다.

    is 연산자는 어떤 오브젝트가 특정한 데이터 타입, 클래스 또는 인터페이스와 일치하는지 판별합니다.
    반면, instanceof 연산자는 어떤 표현식의 프로토타입 체인이 특정한 함수의 프로토타입 오브젝트를 포함하는지 판별합니다. (쓰면서도 뭔 말인지 잘 이해는 안됨 ㅠㅠ)
    어쨌든 어떤 객체가 어떤 클래스에 속하는지 아닌지를 알고자 할 때 사용할 수 있습니다.

    그러나 instanceof는 인터페이스를 인식하지 못하기 때문에 is를 사용하는 것이 더 좋을것 같네요.
    게다가 컴파일 시에도 instanceof대신 is를 사용하라고 태클을 걸어오고요.

    import flash.display.*;
    import flash.events.IEventDispatcher;
    
    var mySprite:Sprite = new Sprite();
    
    trace(mySprite is Sprite); // true
    trace(mySprite is DisplayObject); // true
    trace(mySprite is IEventDispatcher); // true
    
    trace(mySprite instanceof Sprite); // true
    trace(mySprite instanceof DisplayObject); // true
    trace(mySprite instanceof IEventDispatcher); // false
  • onReleaseOutside in AS3

    ActionScript 3.0에서 onReleaseOutside를 대체할 만한 이벤트가 없습니다.
    그래서 MOUSE_DOWN 이벤트가 발생할 때, stage에 MOUSE_UP 이벤트를 추가했다가,
    MOUSE_UP 이벤트 핸들러에서 다시 MOUSE_UP를 제거시키는 방법을 사용해야 하네요.

    public function Test(){
            // Add MOUSE_DOWN event to mc
            mc.addEventListener(MouseEvent.MOUSE_DOWN, rotateDragStart);
    }
    
    private function rotateDragStart(evt:MouseEvent):void{
            // Start drag
    
            // Add ENTER_FRAME event to mc
            mc.addEventListener(Event.ENTER_FRAME, rotateDragging);
            // Add MOUSE_UP event to stage
            stage.addEventListener(MouseEvent.MOUSE_UP, rotateDragStop);
    }
    private function rotateDragStop(evt:MouseEvent):void{
            // Stop drag
    
            // Remove ENTER_FRAME event from mc
            mc.removeEventListener(Event.ENTER_FRAME, rotateDragging);
            // Remove MOUSE_UP event from stage
            stage.removeEventListener(MouseEvent.MOUSE_UP, rotateDragStop);
    }
    private function rotateDragging(evt:Event):void{
            // Excute while dragging
    }

    thanks to derision, kidari70 and plandas from flash8coders.com