1 Reply Latest reply on Jun 9, 2006 7:08 AM by kglad

    Add buttons at runtime and attach events handlers to them

    Level 7
      Hello.

      I need to create several buttons at runtime. I get the buttons data from
      a loaded XML file like this:

      <root>
      <node1>
      <img> http://www.mysite.com/oneimage.jpg</img>
      </node1>
      <node2>
      <img> http://www.mysite.com/anotherimage.jpg></img>
      </node2>
      </root>

      With this XML I'll have to create two buttons:

      var btn:Button;
      var btnName:String;
      for ( var i:Number = 0; i < xmlNodes.length; i++ )
      {
      btnName = "node" + i + "_btn";
      _root.attachMovie("node_btn",btnName, _root.getNextHighestDepth());
      btn = eval("_root." + btnName);
      btn._y = 375;
      btn._x = ((btn._width + 5) * i);
      }

      Now I want to add an onRelease event handler for these buttons so when
      each button is pressed the img url will be showed somewhere in the window:

      node0_btn.onRelease = funcion() {
      loadImg(0);
      }

      function loadImg(nodeIndex:Number) {
      trace(_root.nodes[nodeIndex]["img"]);
      }

      How can I do this with actionscript?

      Thanks.

      Bye.