package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.*;
import com.adobe.serialization.json.*;
public class ScreenCategories extends Sprite {
public var myLoader:URLLoader;
public var filePath:String;
public var myReq:URLRequest;
public function ScreenCategories() {
// constructor code
reLoad();
}
// Constructor: Create an array of three categories
public function reLoad()
{
lastButtonEndedY = 35;
/*
In our real app, we would load in the categories from our database (via a JSON)
Hint: Saving the Objects into the array at the index that equals the ID is advised
*/
filePath = "getCategories.php"; //declare path and file name
myReq = new URLRequest(filePath); //create URLRequest to access the file
myLoader = new URLLoader(); //create URLLoader to load the file
//add event listener to run fileLoaded function when loading is complete
myLoader.removeEventListener(Event.COMPLETE, loadComplete);
myLoader.addEventListener(Event.COMPLETE, loadComplete);
//add event listener to listen for any error thrown during loading process
myLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
myLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
myLoader.load(myReq); //start loading a file with our URLLoader instance
}
//loading complere so print content of a text file to our Text Field
function loadComplete(e:Event):void
{
var jsonstring:String = myLoader.data;
var categories:Array = JSON.decode(jsonstring);
for (var count:int = 0; count < categories.length; count++) {
var aCategory:Object = category[count];
trace(aCategory.categoryname);
trace(aCategory.categoryid);
}
///////////////
// Consider that it would be more responsible to add these to an array and maintain
// which are being added/deleted, then we could create a print function!
///////////////
}
//if any error was thrown, event was also dispatched and now you can print
//the error description to the text field.
public function ioErrorHandler(e:IOErrorEvent):void
{
trace("There was a problem loading "+filePath+": "+e);
}
}
}
// for each "category" in our list (Array)...
for (var count in categories)
{
// Create a button for each of the categories that exist in our Array
var aCategory:BtnCategory = new BtnCategory(categories[count].category);
// Add the BtnCategory to the stage
aCategory.x = 0;
aCategory.y = lastButtonEndedY;
aCategory.name = categories[count].id; // give it a unique name!
addChild(aCategory);
lastButtonEndedY += (aCategory.getHeight() + 1);
}
addEventListener(MouseEvent.CLICK, mouseClicked);
这应该适用于移动应用程序的动作脚本 3,我需要一个类别列表,将我带到产品列表和产品....但我首先在读取值的类别列表上工作从 JSON,我已经有一个 getCategory.php 文档,其中我的 json 被正确返回......但是当我运行 swf 时它说错误......