1

在此处输入图像描述

您好,我决定使用另一张图片来展示我的目标,我想知道是否可以在成功拖放时加载多个 XML 文件?是否可以在取消目标时重置或切换到空白 XML?

我的代码已经和我的其他代码混在一起了,基本上如果圆圈是按钮,每次都会加载不同的XML,我想或多或少地合并两组代码,这样当圆圈/雕像被拖过去时,“ Circle one In Place Successfully”消息将出现,并且我拥有的 xml 文件也将加载。当我试图实现它时,可悲的是,总体结果是无数错误,这让我不敢说。

哦,我也想清楚这一点,这不是我之前关于拖放的问题的抵消,这更多的是我尝试不同的方法并学习一般的 XML,所以我最终使用它作为如果可以在每次拖动时加载不同的 XML,则用于测试情况的​​“骨架”。

import flash.net.URLLoader;
import flash.events.Event;

var circles:Array = [circle1_mc, circle2_mc, circle3_mc];

circle1_mc.myname = "xmlFile1";
circle2_mc.myname = "xmlFile2";
circle3_mc.myname = "xmlFile3";

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML(); 

function LoadXML (evt:Event):void
{
    trace ("Load completed");
    xmlData = XML(xmlLoader.data);   
}

var bArray:Array = [circle1_mc, circle2_mc, circle3_mc];
for (var k:Number = 0; k<bArray.length; k++)
{
    bArray[k].buttonMode = true;
    bArray[k].addEventListener (MouseEvent.CLICK,changeXMLPath);
}
function changeXMLPath (evt:MouseEvent):void
{
    for (var i:Number = 0; i<bArray.length; i++)
    {
        if (evt.target.myname == bArray[i].myname)
        {         
            xmlLoader.load (new URLRequest(evt.target.myname+".xml"));
            xmlLoader.addEventListener (Event.COMPLETE,LoadXML);
        }
    }
}



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
{
    reply_txt.text = "";
    event.target.startDrag(true);
    event.target.parent.addChild(event.target);
}

function dropIt(event:MouseEvent):void
{
    event.target.stopDrag();
    if(event.target.hitTestObject(target_mc)){
        event.target.buttonMode = false;
        event.target.x = target_mc.x;
        event.target.y = target_mc.y;


        for each(var circleMC:MovieClip in circles)
        {
            if(event.target != circleMC)
            {
                circleMC.x = circleMC.startX;
                circleMC.y = circleMC.startY;
            }
            if(event.target.name == "circle1_mc"){
                reply_txt.text = "Statue1 In Place Successfully";

            };
            if(event.target.name == "circle2_mc"){
                reply_txt.text = "Statue2 In Place Successfully";
            };
            if(event.target.name == "circle3_mc"){
                reply_txt.text = "Statue3 In Place Successfully";
            };
        }
    }
    else
    {
        event.target.x = event.target.startX;
        event.target.y = event.target.startY;
        event.target.buttonMode = true;
    }
}
4

1 回答 1

1

您需要使用startDrag()stopDrag()方法以及hitTestObject()方法如下,

  • 我在舞台上放置了三个圆圈,分别为“circle1”、“circle2”和“circle3”。
  • 我还在舞台上放置了三个符号作为放置目标,实例名称为“dropTarget1”、“dropTarget2”和“dropTarget3”。

将以下代码放在 Flash 时间轴上,

var totCircles:int = 3;
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML(); 

for (var k:Number = 1; k<=totCircles; k++)
{
   this["circle" + k].buttonMode = true;
   this["circle" + k].addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
   this["circle" + k].addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}

function startDragging(e:MouseEvent):void
{
     e.currentTarget.startDrag(false);
}

function stopDragging(e:MouseEvent):void
{
    // extract index from current target
    var currTarIndex = e.currentTarget.name.slice(6); 
    e.currentTarget.stopDrag();

    if(e.currentTarget.hitTestObject(this["dropTarget" + currTarIndex]))
    {
         loadXML("xmlFile"+ currTarIndex + ".xml");
         //You can also add position snap here
    }
 }

 function loadXML(url:String):void
 {
      xmlLoader.load (new URLRequest(url));
      xmlLoader.addEventListener (Event.COMPLETE,LoadXML);
 }

希望这会帮助你。

于 2013-03-13T20:22:07.807 回答