A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Varying XML on Button Press

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3

    Question Varying XML on Button Press

    Hello,

    Sorry if this has been asked before, I couldn't find anything using the search bar though.

    I am creating an heirarchical menu in flash, based upon an XML file. There are 5 or 6 buttons, each with a different label loaded from the XML file. Upon pressing one of these buttons, I want the button labels to change to the relevant submenu's labels. Whilst I could do this by having an individual frame for each submenu, this wouldn't be ideal as I want to be able to load different XML files in from time to time, which may have a different structure.

    I was wondering whether anyone has any idea how I could do this?

    For each node on the XML there is a variable called "cmdID". This matches up to the ID of a particular submenu. So for example, the item I have put in bold matches up to the submenu node which I have also put in bold.

    I assume I can perform some sort of navigation based around this, but I am stumped.

    If anyone can help that'd be great thanks.

    An example of the XML is below:

    - <menu label="Main" id="1" childindex="1" parent="0">
    <item voice="No" child="0" cmdID="2" index="1" menutype="2" value="Main Menu">Main Menu</item>
    <item voice="No" child="3" cmdID="3" index="2" menutype="1">Sub Menu 1</item>
    <item voice="No" child="24" cmdID="24" index="3" menutype="1">Item 2</item>
    <item voice="No" child="30" cmdID="30" index="4" menutype="1">Item 3</item>
    <item voice="No" child="72" cmdID="72" index="5" menutype="1">Item 4</item>
    <item voice="No" child="100" cmdID="100" index="6" menutype="1">Item 5</item>
    </menu>

    <menu label="Sub Menu 1" id="3" childindex="2" parent="1">
    <item voice="No" child="0" cmdID="4" index="1" menutype="2">Sub Menu 1</item>

  2. #2
    Junior Member
    Join Date
    Jun 2010
    Location
    Boulder, CO
    Posts
    9

    Are you able to use Actionscript

    Dear geetarguy,
    Have you used Actionscript much? It will do the job pretty easily.
    See if the code below can help you.

    Look at this sample code to create a button

    private function addButton(label:String, x:int, y:int, dx:int, dy:int, toolTip:String):Button {
    var mN:String = cN+".addButton " + tr.enquote(label) + tr.locationToString(x,y) + tr.sizeToString(dx,dy);
    if (tr.traceSemanticCanvasButtons) tr.output(mN);

    var button:Button = new Button();
    button.label = label;
    button.x = x;
    button.y = y;
    button.width = dx;
    button.height = dy;
    button.addEventListener(MouseEvent.CLICK,buttonEve nt);
    button.addEventListener(MouseEvent.DOUBLE_CLICK,bu ttonEvent);
    if (toolTip) button.toolTip = toolTip;

    if (tr.traceSxBorderContainer) tr.output(mN);
    semanticCanvas.addElement(button);

    return button;
    }

    And here is some code to loop through:
    button = addButton(label, x, y, dxButton, dyButton, book.toolTip);
    button.width = dxButton
    x += button.width + dxButtonSpacing;

    oneButtonArray = new Array(button,label,titleBook,book);
    allButtons.push(oneButtonArray);


    In the array "oneButtonArray" (one array for each button), I keep track of the button, label, book title, and book object. Then my button mouse event handler looks like this:
    private function buttonEvent(mouseEvent:MouseEvent):void {
    var eventButton:Button = mouseEvent.target as Button;

    switch (eventButton) {

    default:
    var oneButtonArray:Array;
    var button:Button, label:String, url:String;
    var book:SxBook;
    if (eventButton) {
    for each (oneButtonArray in allButtons) {
    button = oneButtonArray[0];
    if (eventButton == button) { // was this the button that was clicked?
    label = oneButtonArray[1];
    book = oneButtonArray[3];

    selectBook(book);
    book.selectFirstSection(semanticCanvas);
    break;
    }
    }
    }
    break;
    }
    }

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Location
    Boulder, CO
    Posts
    9
    Also - you can ignore all statements with "tr." in them, since they are for debug only.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks for that 4dtext, I'll try it out in a min. Just out of curiosity, is that AS2 or AS3 code? I've done a bit of AS2 but not much concerning arrays.

    Thanks

    Ern

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Hey,

    I can't get it to work unfortunately =(
    I'm getting "Attribute used outside class" errors, for the two lines of code below:


    private function addButton(label:String, x:int, y:int, dx:int, dy:int, toolTip:String):Button {

    private function buttonEvent(mouseEvent:MouseEvent):void {


    I've tried to work it out but I can't see why this error would be produced. Would you have any idea at all?

    Thanks

  6. #6
    Junior Member
    Join Date
    Jun 2010
    Location
    Boulder, CO
    Posts
    9

    Compile errors

    The code that I supplied is quite "bland" but it is AS3 and works under Flex Builder 4.

    The error that you mention involves the context of the code, and not the code itself. I define those functions inside a class that subclasses a BorderContainer.
    You may not be that far along...

    Please post your complete "index.mxml" file (or similar name) that is the root of your application. Are you using Flex Builder? Command-line compile?
    Oz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center