Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
GreenSock

Copy or clone an object (sorta)

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 assume you've got the item(s) selected, right? It should be as simple as:

 

myManager.deleteSelection();

Link to comment
Share on other sites

No, not all aren't selected. Picture sever shapes being copied to make a bigger shape. I have a refresh button to clear the pen tool and any changes made during the activity. I also need any newCopy that was dragged from the original to be cleared.

Link to comment
Share on other sites

I don't quite understand your question, but TransformItem has a deleteObject() method and TransformManager has a deleteSelection() method - you should be able to do pretty much anything with those.

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

Yes! Thanks! I was so close to having that, but I was missing a couple components.

Link to comment
Share on other sites

  • 4 weeks later...
Crumbs. That's neat !

 

+1 That really IS neat. :)

Link to comment
Share on other sites

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.
×