Jump to content
Search Community

Copy or clone an object (sorta)

GreenSock test
Moderator Tag

Recommended Posts

I was e-mailed a question about this and I figured some others might want to do something similar, so I figured I'd post the solution.

 

The goal was to make a generic copy of an object so that you can click on it and drag out a duplicate (which isn't exactly a clone, but it is a new instance of the same class and it has the same transformation properties like x/y/scaleX/scaleY). Here's some very simple code that should serve as a good jumping-off point:

 

var manager:TransformManager = new TransformManager();

original_mc.addEventListener(MouseEvent.MOUSE_DOWN, makeCopy);

function makeCopy(event:MouseEvent):void {
var newCopyType:Class = getDefinitionByName(getQualifiedClassName(event.currentTarget)) as Class;
var newCopy:DisplayObject = new newCopyType();
newCopy.transform.matrix = event.currentTarget.transform.matrix; //copies x, y, scaleX, scaleY, and rotation
addChild(newCopy);
var item:TransformItem = manager.addItem(newCopy);
newCopy.dispatchEvent(event); //makes things act like the user moused-down on the newCopy which means TransformManager will select it and start dragging it. 
}

Link to comment
Share on other sites

I guess my question wasn't very clear. Sorry. I used the code above. Works great. Now the user needs to be able to clear all copies. Below where the question marks are is where I am not clear. I tried removeChild, deleteSelection, deleteObject, etc. (note: this is used on a interactive whiteboard lesson)

 

MovieClip(root).lesson_mc.toolbar_mc.reset_mc.addEventListener(MouseEvent.MOUSE_UP, clearCopies);

function clearCopies(Event:MouseEvent):void {

?????????????????????????

 

}

Link to comment
Share on other sites

Oh, I see. You'd need to keep an array of the copies so that you can easily loop through it when you want to delete the objects. Kinda like:

 

var manager:TransformManager = new TransformManager();
var copies:Array = [];
original_mc.addEventListener(MouseEvent.MOUSE_DOWN, makeCopy);

function makeCopy(event:MouseEvent):void {
  var newCopyType:Class = getDefinitionByName(getQualifiedClassName(event.currentTarget)) as Class;
  var newCopy:DisplayObject = new newCopyType();
  newCopy.transform.matrix = event.currentTarget.transform.matrix; //copies x, y, scaleX, scaleY, and rotation
  addChild(newCopy);
  var item:TransformItem = manager.addItem(newCopy);
  copies.push(item);
  newCopy.dispatchEvent(event); //makes things act like the user moused-down on the newCopy which means TransformManager will select it and start dragging it.
}

MovieClip(root).lesson_mc.toolbar_mc.reset_mc.addEventListener(MouseEvent.MOUSE_UP, clearCopies);
function clearCopies(Event:MouseEvent):void {
   var i:int = copies.length;
   while (--i > -1) {
       copies[i].deleteObject();
       manager.removeItem(copies[i]);
   }
}

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...