[카테고리:] Flash

  • 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
  • Alpha property ranges between 0 and 1

    AS2에서는 alpha, scaleX, scaleY 등 속성의 범위가 0-100 이었으나,
    AS3에서는 0에서 1 사이로 바뀌었습니다.

    아직 적응이 안되었는지, 작업하다보면 이게 상당히 헤깔리네요.

  • [AS3] Downloader class

    Downloader.as code

    /*
     * This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.
     * http://creativecommons.org/licenses/by-nc-sa/3.0/
     */
    
    package	hangunsworld.util{
    
    	import	flash.net.FileReference;
    	import	flash.net.URLRequest;
    	import	flash.display.DisplayObject;
    
    	/*
    	 * Downloader
    	 * Downloader class allows you to download a file easily.
    	 *
    	 * @author: Han Sanghun (http://hangunsworld.com/blog)
    	 * @last modified: 2007-03-23
    	 * @language version: ActionScript 3.0
    	 */
    
    	public class Downloader{
    
    		private var file:FileReference;
    		private var req:URLRequest;
    
    		/*
    		 * Downloader Constructor
    		 */
    		public function Downloader(){
    			file = new FileReference();
    			req = new URLRequest();
    		}
    
    		/*
    		 * downloadSWF
    		 * Download an SWF file that contains the provided DisplayObject.
    		 * @param obj A DisplayObject to download.
    		 */
    		public function downloadSWF(obj:DisplayObject):void{
    			req.url = obj.loaderInfo.url;
    			file.download(req);
    		}
    
    		/*
    		 * downloadURL
    		 * Download a specific file.
    		 * @param url An URL of a file to be downloaded.
    		 */
    		public function downloadURL(url:String):void{
    			req.url = url;
    			file.download(req);
    		}
    
    	}
    
    }

    import hangunsworld.util.Downloader;
    
    var dw:Downloader = new Downloader();
    dw.downloadURL("http://somedomain.com/somefile.as");
    dw.downloadSWF(this);
  • Font.hasGlyphs() works with embeded fonts only.

    Specifies whether a provided string can be displayed using the currently assigned font.

    quote from Adobe® Flex™ 2 Language Reference

    According to the AS3 Help, this method let you know whether a specific string can be displayed using the current font, but it didn’t work.
    So I googled.
    And I found that it works only with embeded fonts.
    AS3 도움말에는 지정한 폰트에서 특정 문자열을 표시할 수 있는지 여부를 알려준다고 되어있는데, 실제로 해보니 안되더군요.
    그래서 좀 더 구글해보니…
    임베드된 폰트에서만 사용할 수 있는 메소드였네요 ㅠㅠ

    Flex 2 Developer’s Guide > Setting character ranges