<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-159  評論-114  文章-7  trackbacks-0

    On occasion you may want to create classes dynamically by name. To do this in Flex 3 you can use getDefinitionByName (it's previous incarnation was getClassByName). Here's a quick example:

    Actionscript:
    1. //cc() is called upon creationComplete
    2. private function cc():void
    3. {
    4.         var obj:Object = createInstance("flash.display.Sprite");
    5. }
    6.  
    7. public function createInstance(className:String):Object
    8. {
    9.         var myClass:Class = getDefinitionByName(className) as Class;
    10.         var instance:Object = new myClass();
    11.         return instance;
    12. }

    The docs for getDefinitionByName say:

    "Returns a reference to the class object of the class specified by the name parameter."

    ...so you may be wondering why in the above code we needed to specify the return value as a Class? This is because getDefinitionByName can also return a Function (e.g. 'flash.utils.getTimer' - a package level function that isn't in any class). As the return type can be either a Function or a Class the Flex team specified the return type to be Object and you are expected to perform a cast as necessary.

    The above code closely mimics the example given in the docs, but in one way it is a bad example because everything will work fine for "flash.display.Sprite", but try to do the same thing with a custom class and you will probably end up with the following error:

    ReferenceError: Error #1065: Variable [name of your class] is not defined.

    The reason for the error is that you must have a reference to your class in your code - e.g. you need to create a variable and specify it's type like so:

    Actionscript:
    1. private var forCompiler:SomeClass;

    Without doing this your class will not be compiled in to the .swf at compile time - and there is reason behind this madness. The compiler only includes classes which are actually used (and not just imported). It does so in order to optimise the size of the .swf. So the need to declare a variable should not really be considered an oversight or bug, although it does feel hackish to declare a variable that you don't directly use.

    Here is the code your would need to create a custom class called Person from a string (where Person resides in the top level package along with your default application).

    Actionscript:
    1. //cc() is called upon creationComplete
    2. private var forCompiler:Person; //REQUIRED! (but otherwise not used)
    3.  
    4. private function cc():void
    5. {
    6.         var obj:Object = createInstance("Person");
    7. }
    8.  
    9. public function createInstance(className:String):Object
    10. {
    11.         var myClass:Class = getDefinitionByName(className) as Class;
    12.         var instance:Object = new myClass();
    13.         return instance;
    14. }

     

    You can declare properties for all possible classes that you intend to create but if you are not happy with doing that inside your main app Alex Harui has another suggestion (something I've not personally tried yet):

    "The code for the class has to be in a SWF. It can be in the main SWF or
    loaded via a module later. There is a compiler option (-includes) that
    allows you to stuff other classes into a SWF without explicitly naming
    them in your source code."

    Passing in Parameters

    Another thing you may find yourself wanting to do is to pass parameters to the constructor of your class (indeed, this is what got me on to the subject today). As far as I'm aware this isn't possible to do this in AS3 (and isn't part of the ECMA spec so it is unlikely to change - please leave a comment or email if you know otherwise ), so you may want to set up another method which can accept and set multiple parameters . If you pass in an array you may stumble across another problem - the original array you passed in becomes the first item of a new array ... okay, that probably sounds confusing so see here for a similar example and solution. In short, the solution is to use Function.apply, and you can see how I used it below:

    Actionscript:
    1. private var forCompiler:Person;
    2.  
    3. private function cc():void
    4. {
    5.         var obj:Object = createInstance("Person", ["bob", 30]);
    6. }
    7.  
    8. public function createInstance(className:String, args:Array):Object
    9. {
    10.         var myClass:Class = getDefinitionByName(className) as Class;
    11.         var instance:Object = new myClass();
    12.         instance.initArgs.apply(null, args);
    13.         return instance;
    14. }
    15.  
    16. /*********************************************************************
    17. //In person.initArgs()....
    18. //Note: the constructor also accepts name &amp; age, assigning them default values
    19. //if not specified.
    20. public function initArgs(name:String, age:uint):void
    21. {
    22.         this.name = name;
    23.         this.age = age;
    24. }
    25. *********************************************************************/

    Now, I'm not sure if this is the best approach, but it works, and with a deadline approaching that's good enough for now :]



    u dont have to create a variable, its enough just to reference to the class somewhere in the code;

    package example
    {
    import example.items.*;
    import flash.utils.getDefinitionByName;

    // force import
    example.items.Class1;
    example.items.Class2;

    class Test
    {
    public function Test()
    {
    var test:Class = getDefinitionByName(”example.items.Class1?) as Class;

    var testInstance = new test();
    }
    }
    }



    posted on 2010-02-14 15:40 北國狼人的BloG 閱讀(231) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲免费福利视频| 国产精品无码免费专区午夜| 久久青草91免费观看| 亚洲日韩乱码中文无码蜜桃臀网站| 无遮挡国产高潮视频免费观看| 国产三级免费电影| 日本激情猛烈在线看免费观看| 免费a级毛片18以上观看精品| 老司机亚洲精品影院在线观看| 国产精品视_精品国产免费 | 中文字幕在线免费观看| 亚洲国产精品久久久久网站| 亚洲视频在线免费播放| 亚洲国产精品久久久久秋霞影院| free哆啪啪免费永久| 亚洲中文字幕乱码一区| 国产公开免费人成视频| 国产视频精品免费视频| 亚洲AV人人澡人人爽人人夜夜| 亚洲国产精品免费在线观看| 四虎必出精品亚洲高清| 国产极品粉嫩泬免费观看| 一个人看的www在线免费视频| 亚洲人成人网站色www| 8x8x华人永久免费视频| 国产精品亚洲一区二区麻豆| 亚洲成?Ⅴ人在线观看无码| 免费91最新地址永久入口| 99999久久久久久亚洲| 亚洲国产精品一区二区第一页免 | 精品成人免费自拍视频| 亚洲乱码卡三乱码新区| 国产免费变态视频网址网站| a毛片在线免费观看| 亚洲一区动漫卡通在线播放| 免费a级毛片大学生免费观看| 日本免费一区二区三区四区五六区| 亚洲 日韩经典 中文字幕| 2048亚洲精品国产| 91九色精品国产免费| 美女被暴羞羞免费视频|