as3.0 xml加载图片,如何点击显示图片说明
as3.0 用xml载入图片,不知道怎么在鼠标点击图片时获得xml中的其他数据
//图片信息数组
var pictureBox:Array = new Array();
//图片数组
var pictures:Array = new Array();
var imgxml:XML=new XML();
var xmlLoader:URLLoader=new URLLoader();
xmlLoader.load(new URLRequest("imgList.xml"));
xmlLoader.addEventListener(Event.COMPLETE,xmlComplete);
function xmlComplete(event:Event):void{
imgxml=XML(xmlLoader.data);
for (var i:uint=0; i<imgxml.product.length(); i++) {
var pictureObj:Object = new Object();
pictureObj.imgSrc = imgxml.product[i].img;
pictureObj.imgText = imgxml.product[i].text;
pictureBox.push(pictureObj);
}
showImages();
}
function showImages():void{
for(var i:uint=0; i<pictureBox.length; i++){
//加载图片
var imgLoader:Loader = new Loader();
addChild(imgLoader);
imgLoader.load(new URLRequest(pictureBox[i].imgSrc));
pictures.push(imgLoader);
pictures[i].x=i%5*150;
pictures[i].y=int(i/5)*155;
pictures[i].addEventListener(MouseEvent.CLICK, pictureClick);
}
}
function pictureClick(event:MouseEvent):void{
//如何在这里trace图片说明
}
<?xml version="1.0" encoding="utf-8" ?>
<products>
<product>
<text>产品001</text>
<img>images/001.jpg</img>
</product>
<product>
<text>产品002</text>
<img>images/002.jpg</img>
</product>
<product>
<text>产品003</text>
<img>images/003.jpg</img>
</product>
</products>
[解决办法]
通过获取当前Image的URI地址与数组存取的地址进行比对,如果相同则输出平配对imageText值!
function pictureClick(event:MouseEvent):void{ var currentImageURI:String = (event.currentTarget as Image).source.toString(); for(var i:uint=0; i<pictureBox.length; i++){ if(pictureBox[i].imgSrc == currentImageURI) { trace(pictureBox[i].imgText); } }}