Share Posted October 20, 2011 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 More sharing options...
Share Posted October 24, 2011 Crumbs. That's neat ! Link to comment Share on other sites More sharing options...
Share Posted October 26, 2011 How do I delete these copies using a button? Link to comment Share on other sites More sharing options...
Author Share Posted October 26, 2011 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 More sharing options...
Share Posted October 31, 2011 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 More sharing options...
Author Share Posted October 31, 2011 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 More sharing options...
Share Posted November 2, 2011 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 More sharing options...
Author Share Posted November 2, 2011 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 More sharing options...
Share Posted November 2, 2011 Yes! Thanks! I was so close to having that, but I was missing a couple components. Link to comment Share on other sites More sharing options...
Share Posted November 26, 2011 Crumbs. That's neat ! +1 That really IS neat. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now