Is it possible to access the type of object being interacted with so I can create a new instance of the same object? So for example in the code below I have the movieclip myItem. After it's clicked it is removed from stage and then moved to the inventory. When I click on it there, is it possible to create a new instance of mcItemToDuplicate using the event information parameters? (while开发者_如何学编程 allowing myItem to be where it is).
My code looks something like this:
public function moveclip() {
var myItem:mcItemToDuplicate = new mcItemToDuplicate();
stage.addChild(myItem);
myItem.addEventListener(MouseEvent.CLICK, pickUp);
}
private function pickUp(e:MouseEvent){
stage.removeChild(MovieClip(e.target));
MovieClip(e.target).removeEventListener(MouseEvent.CLICK, pickUp);
inventory.addChild(MovieClip(e.target));
MovieClip(e.target).addEventListener(MouseEvent.CLICK, useItem);
}
private function useItem(e:MouseEvent){
//??
}
flash.utils package has some methods that you might find useful.
import flash.utils.*;
//use currentTarget; target might be different.
var className:String = getQualifiedClassName(e.currentTarget);
var type:Class = getDefinitionByName(className) as Class;
var obj:Sprite = new type();//datatype of var obj can be Sprite or MovieClip
//or just Object depending on how you plan to use it
something.addChild(obj);
精彩评论