我想使用文本文件动态地将图像添加到flash阶段。使用URLLoader和URLRequest类来访问名为textPic.txt的文本文件。目前我已使用此脚本加载了文本文件:
public class imgLoader extends MovieClip
{
private var myTextloader:URLLoader=new URLLoader();
var fl_TextURLRequest:URLRequest = new URLRequest("textPic.txt");
public function imgLoader()
{
myTextloader.addEventListener(Event.COMPLETE,onloaded);
//myTextloader.load(new URLLoader("textPic.txt"));
myTextloader.load(fl_TextURLRequest);
}
private function onloaded(e:Event):void
{
trace(e.target.data);
}
答案 0 :(得分:2)
import com.adobe.serialization.json.JSON;
var ldr :URLLoader = new URLLoader;
ldr.addEventListener( Event.COMPLETE, loadHandler );
ldr.load( new URLRequest('images.json') );
function loadHandler( e:Event ):void
{
var rawData:String = String(e.target.data);
var imageData:Array = JSON.decode(rawData) as Array;
for (var i:int = 0; i < imageData.length; i++)
{
trace( imageData[i].name + ' url: ' + imageData[i].url );
}
}
images.json:
[
{
"name": "Image 1",
"url": "image1.jpg"
},
{
"name": "Image 2",
"url": "image2.jpg"
},
{
"name": "Image 3",
"url": "image3.jpg"
}
]