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

動的にVectorを生成。ただし、プライベートなクラスは不可

Vector をランタイムに動的生成。
Get Adobe Flash player
by mtok 08 Dec 2009
/**
 * Copyright mtok ( http://wonderfl.net/user/mtok )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/sz1f
 */

/*
Vector をランタイムに動的生成。
*/
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.Event;
    import flash.utils.describeType;
    
    public class FlashTest extends Sprite {
        
        private var _tf:TextField;
        public function FlashTest() {
            // write as3 code here..
            
            addEventListener(Event.ADDED_TO_STAGE, initialize);
        }
        public function initialize(event:Event):void{
            removeEventListener(event.type, arguments.callee);

            _tf = new TextField();
            addChild(_tf);
            _tf.width = stage.stageWidth;
            _tf.height = stage.stageHeight;

            println("Start Application");
            
            
            var vecSprite:* = VectorUtil.createTypedVector(Sprite, 5, true);
            vecSprite[0] = new Sprite();
            
            var desc:XML = XML(describeType(vecSprite));
            println(desc.@name);
            
            //プライベートなクラスはgetDefinitionで得ることができないっぽい。
            
        }
        
        private function println(str:String):void{
            _tf.appendText(str);
            _tf.appendText('\n');
        }
        private function print(str:String):void{
            _tf.appendText(str);
        }      
    }
}

import flash.system.ApplicationDomain;
import flash.utils.getQualifiedClassName;
class VectorUtil {
	static public const VECTOR_QNAME:String = getQualifiedClassName(Vector);
	static public function getVectorDefinition(elementCls:Class, appDomain:ApplicationDomain = null):Class {
		if (!appDomain) {
			appDomain = ApplicationDomain.currentDomain;
		}
		return appDomain.getDefinition(VECTOR_QNAME + ".<" + getQualifiedClassName(elementCls) + ">") as Class;
	}
	static public function createTypedVector(dataType:Class, length:uint = 0, fixed:Boolean = false, domain:ApplicationDomain=null):*{

		var def:Class = getVectorDefinition(dataType, domain);
		return new def(length, fixed);
	}
}