Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

actionscript 3 - how to work registerClassAlias() method for custom mxml components

I have flex mxml custom component(Graphic).According to requirement a need to copy them as copy or cut operation.but problem in the registerClassAlias() method,how it will work for custom graphic or Group(or UIComponents) components.

var className:String = getQualifiedClassName(zorder.getItemAt(0));
            _saveIn.clear();

            registerClassAlias(className, zorder.getItemAt(0) as Class);
            _saveIn   =     SharedObject.getLocal("save");
            _saveIn.data.value1 = new ByteArray();
            _saveIn.data.value1.writeObject(zorder.getItemAt(0));
            _saveIn.data.value1.position = 0;
            _saveIn.flush();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not possible to make a full copy of any display object via ByteArray with registerClassAlias->writeObject->readObject approach. It works only with simple objects, for example data objects (like TextFormat, value objects and so). In any case you have to test the copy method for each type of your object to be sure it works properly.

Example of coping Shape, the simplest display object:

package
{
import avmplus.getQualifiedClassName;

import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.net.registerClassAlias;
import flash.utils.ByteArray;
import flash.utils.getDefinitionByName;


public class astest extends MovieClip
{
    public function astest()
    {
        init();
    }

    private function init():void
    {
        var sh:Shape = new Shape();

        sh.graphics.beginFill(0xFF0000);
        sh.graphics.drawEllipse(100, 100, 100, 70);
        sh.graphics.endFill();

        addChild(sh);

        registerObject(sh);

        var ba:ByteArray = new ByteArray();
        ba.writeObject(sh);
        ba.position = 0;
        var obj:Object = ba.readObject();
        var shCopy:DisplayObject = obj as DisplayObject;

        if(shCopy)
        {
            shCopy.x = shCopy.y = 100;
            addChild(shCopy);
        }

    }

    private function registerObject(obj:Object):void
    {
        try
        {
            var qname:String = getQualifiedClassName(obj);
            var cname:String = qname.split("::").join(".");
            var classs:Class = getDefinitionByName(cname) as Class;
            registerClassAlias(qname, classs);
        }catch(error:Error)
        {
            trace(error.message);
        }
    }
}
}

Output:

   TypeError: Error #1034: Type Coercion failed: cannot convert Object@eae09b9 to flash.geom.Transform.

So, you can try to register the flash.geom.Transform before coping:

registerObject(sh.transform);

but this lead to another error:

ArgumentError: Error #1063: Argument count mismatch on flash.geom::Transform(). Expected 1, got 0

Actually, DisplayObject coping is the old topic and you can google for lots of posts about this by errors I mentioned above (especially the last one), but the answer is: You can't copy display objects in via ByteArray, you need to write custom methods for creating a copy of given TextField, Sprite or VBox and copy all the properties manually.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...