views:

25

answers:

3

Have anyone found any solution for cloning the "Instance name"?

Here is what i mean.

  1. Draw a random shape using flash not the actionscript
  2. Convert it to "Symbol"
  3. Assign the instance name to "cloneMe"

now try to clone it

The code should be something like this.

var newClone:MovieClip = cloneMe.copy()
newClone.x=100
newClone.y=50
addChild(newClone);
+2  A: 

Try using ObjectUtil.clone(cloneMe)

It has been a while since I have dealt with symbols, but that should work.

You can see the documentation for ObjectUtil here.

If you do not have Flex available, you can also implement this function yourself. This is the same code that ObjectUtil.clone() uses:

var buffer:ByteArray = new ByteArray();
buffer.writeObject(value);
buffer.position = 0;
var result:Object = buffer.readObject();
return result;
Alan Geleynse
You need Flex for ObjectUtil, this wouldn't work with Flash CS or a pure AS3 project.
PatrickS
Good point, I updated my answer with how you can implement `ObjectUtil.clone` without Flex. It is actually a very simple implementation and is how Flex does it internally.
Alan Geleynse
that's bad, i only have Flash
Darwin
The code that I posted will work with just Flash. `ObjectUtil` is in Flex, but it's implementation is exactly what I wrote above, so you can just do it in your own function in Flash and not need Flex at all.
Alan Geleynse
That seems to clone all the properties, but how do you then convert the returned object to a MovieClip/DisplayObject? Seem to get a Type Coercion failed when I try to cast it.
TandemAdam
`var copy:DisplayObject = DisplayObject(result);` should do it, you just need to do an explicit cast to the type of the original object.
Alan Geleynse
A: 

I gave an answer in your previous question here: http://stackoverflow.com/questions/4000972/actionscripts-3-clone-movieclip

If you need to end with a MovieClip , you only need to add the resulting Bitmap to a MovieClip instance.

If you want to call a method , just create a Class with a static method. This can only be used to copy graphic data.

public class Utils
{
   public static function clone( cloneMe:MovieClip ):MovieClip
   {
      var mc:MovieClip = new MovieClip();
      var bmd:BitmapData = new BitmapData(cloneMe.width , cloneMe.height );
      bmd.draw( cloneMe);
      var bm:Bitmap = new Bitmap(bmd);

      mc.addChild( bm );
      return mc;
   }
 }

Then in Flash, provided that you've added the Utils class to your Library path. You can do this:

 var newClone:MovieClip = Utils.clone( cloneMe );
 //etc...
PatrickS
Your method is not working like the way I want, that's why. be nice bro
Darwin
I may be mistaken but I can only see two ways to do this , Alan Geleynse's answer with a ByteArray or a Bitmap draw... but no worries, wasn't complaining, just curious! :)
PatrickS
A: 

specifying your symbol as custom class might help

www0z0k