use*_*602 3 flash drag-and-drop actionscript-3

标题或多或少是不言自明的,我一直在研究许多不同的教程,而且我对AS3来说并不是很完美.(上图显示了我的目标)
无论如何,在我看到的大多数在线教程中我注意到,拖放教程要么基于一个对象到一个目标,要么多个对象到多个目标,所以我想知道是否有人能够帮助我并解释如何我可以获得多个对象连接到一个目标.
并且,如果可能的话,使其可切换,例如,当我将对象2拖过时,如果对象1已经在目标上就位,则对象1返回到其原始位置,对象2取代它的位置.
解释这个的一个更简单的方法是说我正在尝试创建一个有三个雕像的游戏,用户可以选择三个中的一个放置在一个设定的目标区域.
如果我说的话没有多大意义,我会道歉,如果这会造成混乱,我会澄清任何事情.这是我目前正在使用的AS3代码.
var startX:int;
var startY:int;
circle1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circle2_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle2_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circle3_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle3_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
function pickUp(event:MouseEvent):void {
startX = event.target.x;
startY = event.target.y;
event.target.startDrag(true);
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var theTargetName:String = "target" + event.target.name;
var theTarget:DisplayObject = getChildByName(theTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == theTarget){
event.target.buttonMode = false;
event.target.x = theTarget.x;
event.target.y = theTarget.y;
}
else{
event.target.x = startX;
event.target.y = startY;
circle1_mc.buttonMode = true;
circle2_mc.buttonMode = true;
circle3_mc.buttonMode = true;
Run Code Online (Sandbox Code Playgroud)
dropTarget您可以使用hitTestObject查看被删除的对象是否"触摸" 而不是检查theTarget.否则,任何已经被丢弃的项目都theTarget可能被报告为dropTarget.此外,由于MovieClip是动态的,您可以在每个实例中存储startX和startY值.
以下修改后的代码将使用单个target_mc作为放置目标.当一个项目被丢弃时,任何其他项目将被移回其原始位置:
// create an array as @David suggested to keep track of your draggable items
var circles:Array = [circle1_mc, circle2_mc, circle3_mc];
for each(var circleMC:MovieClip in circles)
{
circleMC.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circleMC.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circleMC.startX = circleMC.x;
circleMC.startY = circleMC.y;
}
function pickUp(event:MouseEvent):void
{
// no longer need to keep track of startX & startY here because that's already been done up above
event.target.startDrag(true);
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void
{
event.target.stopDrag();
// check to see if the event target is touching target_mc using hitTestObject
if(event.target.hitTestObject(target_mc)){
event.target.buttonMode = false;
event.target.x = target_mc.x;
event.target.y = target_mc.y;
// move all circles OTHER than the current target back to their original positions
for each(var circleMC:MovieClip in circles)
{
if(event.target != circleMC)
{
circleMC.x = circleMC.startX;
circleMC.y = circleMC.startY;
}
}
}
else
{
// only need to move the event target back if it was dropped outside of target_mc
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}
Run Code Online (Sandbox Code Playgroud)