Question

I can only find similar questions to my own, but they don't cover my exact problem, so:

I have a symbol Tom stored as a class in a SWC library (it's a vector graphic). I create a variable called Dick which is a new instance of Tom. I addChild(Dick) to a sprite Harry and addChild(Harry) to the stage. That all works.

How do I then make a copy of Harry and add it to the stage elsewhere?

From other answers on this site calling George=clone(Harry); should work, but it doesn't. Using the following function.

    private function clone(mySprite:Sprite):Sprite
    {
        var SpriteClass:Class = getClass(mySprite);

        return new SpriteClass();

    }// end function

    private function getClass(object:Object):Class
    {
        return getDefinitionByName(getQualifiedClassName(object)) as Class;

    }// end function

I'm becoming terribly confused with this and apologise if I've made any mistakes in this my first post.

Thanks for any help.

Was it helpful?

Solution

Since you have nested classes, there is no shortcut to duplicate them at once. You can create new instance of the library class, but there's no way to create instance with all the added children inside.

Think of duplication as simple instantiation of a class. It will be the same as it is when you first created it. If it's empty at the beginning - it will be empty when you duplicate it.

So if you manually added Dick to Harry, then if you create new Harry it will be empty. You need another Dick instance to be added to the new Harry.

Just create a function like createHarry(). There you instantiate all classes and add them one to each other as you want. Then return the latest Harry class and you are ready to go :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top