A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: functions with arrays

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    29

    Post functions with arrays

    function does not works with array in following code:
    in reference image these five are the movieclips on the stage and their instance names are same as the array items. see attachment for reference.

    Code:
    var mainBtnsArr:Array = ["chapters", "projects", "exercises", "skills", "glossary"]; for (i=0; i<mainBtnsArr.length; i++) { mainBtn = mainBtnsArr[i]; mainBtnsArr[i].onPress = function() { trace("working"); }
    Any help please?

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    where is the attachment?

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    post your fla

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    29
    main.jpg
    sorry my fault..... now please find attachment.

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Here you go, you were on the right track though.
    PHP Code:
    var mainBtnsArr:Array = [chaptersprojectsexercisesskillsglossary];
    for (
    0mainBtnsArr.lengthi++)
    {
        
    mainBtnsArr[i].TheNum = [1];
        
    mainBtnsArr[i].onPress = function()
        {
            
    trace(this.TheNum " " this._name " : " " Pressed");
        };
        
    mainBtnsArr[i].onRelease mainBtnsArr[i].onReleaseOutside = function()
        {
            
    trace(this.TheNum " " this._name " : " " Released");
        };
        
    mainBtnsArr[i].onRollOver = function()
        {
            
    // do something else
        
    };
        
    mainBtnsArr[i].onRollOut mainBtnsArr[i].onDragOut = function()
        {
            
    // do something else
        
    };


  6. #6
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    or if you want to keep you Array items as Strings, you can use this:

    Code:
    var mainBtnsArr:Array = ["chapters", "projects", "exercises", "skills", "glossary"];
    for (i=0; i<mainBtnsArr.length; i++) {
    	btnClick(this[mainBtnsArr[i]]);
    }
    
    function btnClick(btn){
    	btn.onPress = function(){
    		trace("working");
    	}
    }
    the key is this[mainBtnsArr[i]], because it looks for an object with the value of the string. You were only giving a String onPress handler
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    29
    thanks, solved

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