我正在尝试为 children 创建一个游戏,他们可以将字母拖到舞台上来组成单词。
我想添加一个“垃圾桶”,用户可以在其中拖动不再需要的字母来处理它们。我已经创建了影片剪辑,但完全不确定如何使用 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
}
最佳答案
我已经在这里给你解决方案了Flash AS3 Clone, Drag and Drop
在这里,我提供了有关如何将对象拖动到垃圾箱内并将其删除的详细解决方案。
要将复制的对象放入容器内,停止拖动后,检查与容器对象的碰撞。欲了解更多信息,请参阅,
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 = [];
}
}
关于actionscript-3 - AS3 对象箱和重置按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21806857/