AS3 - 通过拖放加载不同的XML文件?

时间:2013-03-13 13:34:17

标签: xml actionscript-3 flash actionscript drag-and-drop

enter image description here

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

我的代码已与我的其他代码混合在一起,基本上如果圆圈是按钮,它会每次加载不同的XML,我想或多或少合并两组代码,以便在拖动圆/雕像时结束,将出现“成功循环播放”消息,并且我将加载我的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;
    }
}

1 个答案:

答案 0 :(得分: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);
 }

希望这会对你有所帮助。