AS3 - 通过拖放加载不同的 XML 文件?
Posted
技术标签:
【中文标题】AS3 - 通过拖放加载不同的 XML 文件?【英文标题】:AS3 - Loading different XML files through drag and drop? 【发布时间】:2013-03-01 11:28:17 【问题描述】:您好,我决定使用另一张图片来展示我的目标是什么,我想知道是否可以在成功拖放时加载多个 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】:您需要使用startDrag()
和stopDrag()
方法以及hitTestObject()
方法,如下所示,
将以下代码放在 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);
希望这会对你有所帮助。
【讨论】:
以上是关于AS3 - 通过拖放加载不同的 XML 文件?的主要内容,如果未能解决你的问题,请参考以下文章