A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] Insert a button for every loop

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    10

    resolved [RESOLVED] Insert a button for every loop

    Hey ho,

    I'm trying to achieve a function that inserts a list of buttons on the stage. The number of the buttons should be determined by a for-loop. This is my code today. As you can see it only returns the result in a text field.

    Code:
    for (var i = 0; i < menuarray.length; i++)
    {
    	xmlbox.text =xmlbox.text+"\n"+this.firstChild.childNodes[9].childNodes[i].attributes.name;    
    }
    Just the clarify: the code I would like some help with should look something like this:

    Code:
    for (var i = 0; i < menuarray.length; i++)
    {
    	insert button, x-cord = 123 and y-cord=456
    }
    I guess there are some nice guide out there, but I can't really find what I am looking for when searching.

    I'm not asking you to do the work for me, but I would be so very thankful if someone could just point me in the right direction.

    Thanks in advice!
    Last edited by Laserkatt; 12-01-2008 at 04:28 AM.

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi Laserkatt,

    Welcome to FlashKit.

    Shouldn't it be something like:
    code:

    for (var i = 0; i < menuarray.length; i++)
    {
    var btn = new MyBtn();
    btn.x = 123;
    btn.y = 456 + i*20;

    addChild (btn);
    }


    ?
    Where MyBtn is the Class associated to the button.

  3. #3
    Junior Member
    Join Date
    Dec 2008
    Posts
    10
    Thank you so very mutch! Now all I have to do is figure out how to create a button class

  4. #4
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    Hi,

    That's done in the library, by rightclicking the movie clip.
    Here's an example.
    Attached Files Attached Files

  5. #5
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    You can use an array to store the links along with a variable associated with each of the buttons.

    code:

    var links:Array = new Array('http://www.google.com/', 'http://www.yahoo.com/','http://www.flashkit.com/','http://www.adobe.com/','http://www.google3.com/','http://www.google3.com/','http://www.google4.com/');

    for (var i = 0; i < 7; i++) {
    var btn = new MyBtn();
    btn.x=0;
    btn.y=0+i*40;
    btn.txtBox.text=i;
    btn.addEventListener(MouseEvent.CLICK, myButtonFunction);

    btn.i = i; // store a variable in each movie clip

    addChild(btn);
    }

    function myButtonFunction(event: MouseEvent) {
    var btn:MovieClip = MovieClip(event.currentTarget);
    trace (btn.i);

    var link:String = links[btn.i]; // grab the variable stored in the movie clip
    trace (link);
    /* uncomment this
    var request:URLRequest=new URLRequest(link);
    navigateToURL(request);
    */
    }


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