我正在尝试为孩子们制作一款游戏,他们可以将字母拖到舞台上制作文字。
我想添加一个'垃圾桶',用户可以拖动他们不再需要处理它们的字母。我已经创建了影片剪辑,但完全不确定如何使用AS3使其正常运行。
我还想添加一个重置按钮,以便舞台恢复到原始状态。再一次,我已经把它拉了起来,并添加了我知道的小as3(使它成为一个按钮),但如果有人可以协助如何实现这一点,我将不胜感激。
import flash.display.MovieClip;
for (var i=1; i<27; i++)
{
this["object" + i].addEventListener(MouseEvent.MOUSE_DOWN, onStart);
this["object" + i].addEventListener(MouseEvent.MOUSE_UP, onStop);
}
var sx = 0,sy = 0;
function onStart(e)
{
sx = e.currentTarget.x;
sy = e.currentTarget.y;
e.currentTarget.startDrag();
}
function onStop(e)
{
if ( e.target.dropTarget != null &&
e.target.dropTarget.parent == dest &&
e.currentTarget.name != "copy" )
{
var objectClass:Class =
getDefinitionByName(getQualifiedClassName(e.currentTarget)) as Class;
var copy:MovieClip = new objectClass();
copy.name = "copy";
this.addChild(copy);
copy.x = e.currentTarget.x;
copy.y = e.currentTarget.y;
e.currentTarget.x = sx;
e.currentTarget.y = sy;
copy.addEventListener(MouseEvent.MOUSE_DOWN, onStart);
copy.addEventListener(MouseEvent.MOUSE_UP, onStop);
}
e.currentTarget.stopDrag();
}
resetButton.addEventListener(MouseEvent.CLICK, reset);
resetButton.buttonMode = true;
function reset(event:MouseEvent):void
{
//Not sure what AS3 to add here to reset to original state
}
答案 0 :(得分:2)
我已经在这里给了你解决方案Flash AS3 Clone, Drag and Drop
在这里,我提供了一个关于如何在bin中拖动对象并将其删除的详细解决方案。
为了在bin中删除复制的对象,停止拖动后,检查与bin对象的碰撞。有关更多信息,请参阅,
copiedObject.hitTestObject(binObject)
例如
首先在舞台上创建垃圾箱MovieClip
并为其指定实例名称'trashCan',然后将以下行添加到onStop()
(e.currentTarget.stopDrag();
下方)功能中,如下所示:
更新:
var copiedObjsArr:Array = [];
function onStop(e)
{
if ( e.target.dropTarget != null &&
e.target.dropTarget.parent == dest &&
e.currentTarget.name != "copy" )
{
//Code here remains same
//.......
//Keep collecting copied letters for further access in `reset()` function
copiedObjsArr.push(copy);
}
else if(e.currentTarget.name == "copy") //this is 'else if' (newly added)
{
var tarObject:MovieClip = e.currentTarget;
// These detects collision of dragged object with the trashCan
if(tarObject.hitTestObject(trashCan)) {
//These removes dragged object from the display list (not from the memory)
removeChild(tarObject);
tarObject = null; //to garbage
}
}
e.currentTarget.stopDrag();
}
你的reset()
变得如此:
function reset(event:MouseEvent):void
{
if(copiedObjsArr.length > 0)
{
//Traverse through all copied letters
for(var i:int = 0; i<copiedObjsArr.length; i++)
{
var objToRemove:MovieClip = copiedObjsArr[i];
removeChild(objToRemove);
objToRemove = null;
}
//Finally empty the array
copiedObjsArr = [];
}
}