[code]
//data.json
{
"items": [{
"img_url": "imgs/anjuke.png",
"link_url": "http://www.amyflash.com",
"intro": "it is a message"
}, {
"img_url": "imgs/yile.png",
"link_url": "http://www.amyflash.com",
"intro": "it is a message"
}, {
"img_url": "imgs/ctrip.png",
"link_url": "http://www.amyflash.com",
"intro": "it is a message"
}, {
"img_url": "imgs/lifevc.png",
"link_url": "http://www.amyflash.com",
"intro": "it is a message"
}, {
"img_url": "imgs/xdf.png",
"link_url": "http://www.amyflash.com",
"intro": "it is a message"
}, {
"img_url": "imgs/taoche.png",
"link_url": "http://www.amyflash.com",
"intro": "it is a message"
}, {
"img_url": "imgs/fanke.png",
"link_url": "http://www.amyflash.com",
"intro": "it is a message"
}
]
}
[/code]
package com.acme.jooflash { import flash.display.DisplayObject; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.external.ExternalInterface; import flash.net.URLLoader; import flash.net.URLRequest; import flash.text.TextField; import joo.getQualifiedObject; import js.Document; /** * ... * @author harriet */ [SWF( backgroundColor='0xFFFFFF', frameRate='30', width='640', height='480')] public class Main extends Sprite { private var sw:Number = 800; private var sh:Number = 700; private var dataloader:URLLoader = new URLLoader(); private var count:Number = 0; private var top:int = 100; private var left:int = 100; public function Main() { if (ExternalInterface.available) { sw = ExternalInterface.call("getw"); sh = ExternalInterface.call("geth"); } this.stage.stageWidth = sw; this.stage.stageHeight = sh; var surl:String = "data.json"; var surlr:URLRequest = new URLRequest(surl); dataloader.load(surlr); dataloader.addEventListener(Event.COMPLETE, getData); } private function getData(e:Event):void { var sMyString:String = dataloader.data; var t:Object = window['JSON'].parse(sMyString); count = t.items.length; for each(var item in t.items ) { var url:String = item.img_url; var urlr:URLRequest = new URLRequest(url); var loader:Loader = new Loader(); loader.load(urlr); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, doShow); } } private var right:Number; private var viewi:int = 0; private var pviewi:int = 0; private var dx:int = 10; private var dy:int = 10; private var pline:int = -1; private function doShow(e:Event):void { var t:DisplayObject = (e.target.loader as Loader).content; right = sw - left - t.width; addChild(t); t.x = left + (t.width + dx) * viewi++; t.y = top; if (t.x > right) { if(pline==-1) { pline = viewi-1; trace(pline);//求出多少列 } pviewi = viewi-1; if (pline != 0) { t.y = top+Math.floor(pviewi / pline) * (t.height + dy); t.x = left + (t.width + dx) * (pviewi % pline); }else { t.x = 5; t.y = 5 + pviewi * (t.height + dy); } } } } }