我有一个包含8个影片剪辑的数组,它们可以被拖放到一个MC上,这是它们共同的hitObject。
我希望无论哪个mc被拖拽添加为hitObject mc的子时,它被丢弃,但是我有麻烦设置代码。目前,只有一个特定的实例将被添加为hitObject的子实例,因为除了特定的实例名之外,我不知道在addChild()参数中应该写什么(以下都是不可接受的:e.a target,数组名,MovieClip名)。
这是我的代码——任何和所有的帮助将是最感激的:
import flash.events.MouseEvent;
import flash.display.MovieClip;
var redArray:Array = [red,red1,red2,red3,red4,red5,red6,red7];
redArray.forEach(setupDrag);
function setupDrag(dragger:MovieClip, index:int, array:Array):void {
dragger.addEventListener(MouseEvent.MOUSE_DOWN, dragRed);
dragger.buttonMode=true;}
redArray.forEach(setupDrop);
function setupDrop(dropper:MovieClip, index:int, array:Array):void {
dropper.addEventListener(MouseEvent.MOUSE_UP, dropRed);
dropper.buttonMode=true;}
var dirt:MovieClip
function dragRed(e:Event):void{
dirt = e.currentTarget as MovieClip;
e.target.startDrag();
}
function dropRed(e:Event):void{
e.target.stopDrag();
if (e.target.hitTestObject(drawer_mc))
{
drawer_mc.addChild(red1);
red1.y=10;
}
}
提前感谢!
你就快成功了。您想将target
或currentTarget
作为参数传递给addChild
。我认为下面的内容应该可以做到(我刚刚发布了我更新的部分)。
这里很好地解释了target
和currentTarget
之间的差异。
var dirt:MovieClip;
function dragRed(e:Event):void {
// sounds like currentTarget and target will both work in your
// case, but stick to one or the other for consistency
dirt = e.currentTarget as MovieClip;
dirt.startDrag();
}
function dropRed(e:Event):void{
// assigning the currentTarget to your dirt variable means you don't
// keep needing to refer to e.currentTarget throughout the function
dirt = e.currentTarget as MovieClip;
dirt.stopDrag();
if (dirt.hitTestObject(drawer_mc)) {
// now you're always adding the instance that triggered
// the mouse up event
drawer_mc.addChild(dirt);
dirt.y=10;
}
}
您已经通过事件目标属性访问了被删除的对象。将其转换为DisplayObject或MovieClip,然后将其传递给addChild()方法:
function dropRed(e:Event):void
{
var droppedObject:MovieClip = e.currentTarget as MovieClip;
droppedObject.stopDrag();
if(droppedObject.hitTestObject(drawer_mc))
{
drawer_mc.addChild(droppedObject);
droppedObject.y=10;
}
}
- e。target指的是被点击的对象。
- e。currentttarget指的是应用了addEventListener()的对象。