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から、jsonが正しく返されたgetCategory.phpドキュメントがすでにありますが、swfを実行すると、エラーが表示されます。