Actionscript 3 XML Menu With For Loop
I am trying to make a Xml Image Menu that on button click goes to specific frame label.
The Loop works, the Images and Text Loads without problems.
My Problems:
The items don't center in stage (only the last xml item);
On button click all the buttons goes to the same Label (the label that is on the last xml item);
MY CODE:
Variables:
//CREATE VARIABLES FOR XML
var columns:Number;
var my_total:Number;
var my_menu:XMLList;
var item_label:String;
var x_counter:Number = 0;
var y_counter:Number = 0;
Load Xml, I Explain in comments everythings that works or not:
//LOAD XML
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("xml.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void
{
var myXML:XML = new XML(e.target.data);
//DEFINE THE LENGTH OF THE MENU ITEMS (WORKS WELL)
my_menu = myXML.ITEM;
my_total = my_menu.length();
columns = myXML. @ COLUMNS;
for (var i:Number = 0; i < my_total; i++)
{
//CREATE THE LOOP FOR MOVIE CLIP THAT I HAVE IN LIBRARY WITH THE CLASS "menu_item"
var MC:MovieClip = new menu_item();
var loadIMG:Loader = new Loader;
// trace(MC); GIVE ME ALL THE XML ITEMS SO WORKS GOOD
addChild(MC);
//To CENTER ALL OBJECTS IN MC (NOT WORKS-开发者_如何转开发 ONLY CENTERS THE LAST ITEM OF THE XML)
function resizeHandler(e:Event):void
{
MC.x = (MC.stage.stageWidth / 2) - (MC.width / 2);
MC.y = (MC.stage.stageHeight / 2) - (MC.height / 2);
}
stage.addEventListener(Event.RESIZE, resizeHandler);
stage.dispatchEvent(new Event(Event.RESIZE));
//DISPLAY MENU ITEMS IN A GRID WITH COLUMNS (WORKS WELL)
MC.x = (MC.width+10)*x_counter;
MC.y = (MC.height+100)*y_counter;
if (x_counter + 1 < columns)
{
x_counter++;
}
else
{
x_counter = 0;
y_counter++;
}
//TO LOAD IMAGES AND TEXT FOR THE MENU (WORKS WELL)
loadIMG.load(new URLRequest (my_menu[i].@IMAGE));
MC.container_mc.img_mc.addChild(loadIMG);
MC.container_mc.title_mc.text = my_menu[i]. @ TITLE;
//TEST
MC.name = "mc" + i;
loadIMG.name = "image" + i;
//ON BUTTON CLICK GO TO LABEL NAME IN XML (NOT WORKS, ALL ITEMS GOES TO THE SAME AND LAST LABEL IN XML)
MC.addEventListener(MouseEvent.CLICK, callFull);
//var test = my_menu[i.target.name].@LABEL; //(FAILED TEST TO TARGET THE LABELS)
var go_labels = my_menu[i]. @ LABEL;
function callFull(e:MouseEvent):void
{
labels_mc.gotoAndStop(go_labels);
}
}
}
XML:
<MENU COLUMNS="2">
<ITEM IMAGE="img/1.jpg" TITLE="title 1" LABEL="label 1"></ITEM>
<ITEM IMAGE="img/2.jpg" TITLE="title 2" LABEL="label 2"></ITEM>
<ITEM IMAGE="img/3.png" TITLE="title 3" LABEL="label 3"></ITEM>
<ITEM IMAGE="img/4.png" TITLE="title 4" LABEL="label 4"></ITEM>
</MENU>
Thanks
I found a couple of errors in your code, tried to fix them quickly, see below and let me know how it works now.
//CREATE VARIABLES FOR XML
var columns:Number;
var my_total:Number;
var my_menu:XMLList;
var item_label:String;
var x_counter:Number = 0;
var y_counter:Number = 0;
//LOAD XML
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("xml.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void
{
var myXML:XML = new XML(e.target.data);
//DEFINE THE LENGTH OF THE MENU ITEMS (WORKS WELL)
my_menu = myXML.ITEM;
my_total = my_menu.length();
columns = myXML. @ COLUMNS;
for (var i:Number = 0; i < my_total; i++)
{
//CREATE THE LOOP FOR MOVIE CLIP THAT I HAVE IN LIBRARY WITH THE CLASS "menu_item"
var MC:MovieClip = new menu_item();
MC.id = i;
MC.label = my_menu[i]. @ LABEL;
MC.name = "mc" + i;
//DISPLAY MENU ITEMS IN A GRID WITH COLUMNS (WORKS WELL)
MC.x = (MC.width+10)*x_counter;
MC.y = (MC.height+100)*y_counter;
if (x_counter + 1 < columns)
{
x_counter++;
}
else
{
x_counter = 0;
y_counter++;
}
var loadIMG:Loader = new Loader;
loadIMG.name = "image" + i;
MC.container_mc.img_mc.addChild(loadIMG);
MC.container_mc.title_mc.text = my_menu[i]. @ TITLE;
addChild(MC);
//ON BUTTON CLICK GO TO LABEL NAME IN XML (NOT WORKS, ALL ITEMS GOES TO THE SAME AND LAST LABEL IN XML)
MC.addEventListener(MouseEvent.CLICK, callFull);
//TO LOAD IMAGES AND TEXT FOR THE MENU (WORKS WELL)
loadIMG.load(new URLRequest (my_menu[i].@IMAGE));
}
stage.addEventListener(Event.RESIZE, resizeHandler);
stage.dispatchEvent(new Event(Event.RESIZE));
}
function callFull(e:MouseEvent):void
{
//e.currentTarget should return the MC instance you clicked
e.currentTarget.labels_mc.gotoAndStop(e.currentTarget.label);
}
//To CENTER ALL OBJECTS IN MC (NOT WORKS- ONLY CENTERS THE LAST ITEM OF THE XML)
function resizeHandler(e:Event):void
{
var i:uint = 0;
for (; i < my_total; i++ )
{
this["mc" + i].x = stage.stageWidth / 2 - this["mc" + i].width / 2;
this["mc" + i].y = stage.stageHeight / 2 - this["mc" + i].height / 2;
}
}
精彩评论