how to work registerClassAlias() method for custom

2019-01-27 04:13发布

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();

2条回答
啃猪蹄的小仙女
2楼-- · 2019-01-27 04:19

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.

查看更多
够拽才男人
3楼-- · 2019-01-27 04:33

Ok, this blog post has a simple solution... you use getDefinitionByName():

So something like this in your code:

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

            registerClassAlias(className, getDefinitionByName(className) 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();
查看更多
登录 后发表回答