A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Initialising an array of mcs variable problem

  1. #1
    Senior Member
    Join Date
    Feb 2001
    Posts
    233

    Initialising an array of mcs variable problem

    Code:
    var str:String;
    for(i=0; i<13; i++) {
    	for(j=0; j<21; j++) {
    		theScene["mask_"+i+"_"+j].onRollOver = function() {
    			str = "mask_"+i+"_"+j;
    			trace(str);
    			theScene[str].gotoAndPlay(2);
    		}	
    	}
    }
    Everytime I run this it prints out mask_13_21, no matter which mask I'm actually on.

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,875
    it's because that is the last known value of i and j. When you rollover any mc it will get the present value of 'i' and 'j' not what the value of i and j was when the onRollover event handler was defined for it. What u need to do is create a dynamic property for the movieclip at the time its onRollover handler is being defined e.g

    Code:
    var str:String;
    for(i=0; i<13; i++) {
    	for(j=0; j<21; j++) {
    
                    theScene["mask_"+i+"_"+j].i = i;
                    theScene["mask_"+i+"_"+j].j = j;
    		theScene["mask_"+i+"_"+j].onRollOver = function() {
    			str = "mask_"+this.i+"_"+this.j;
    			trace(str);
    			theScene[str].gotoAndPlay(2);
    		}	
    	}
    }
    or just

    Code:
    var str:String;
    for(i=0; i<13; i++) {
    	for(j=0; j<21; j++) {
    
    		theScene["mask_"+i+"_"+j].onRollOver = function() {
    			this.gotoAndPlay(2);
    		}	
    	}
    }
    The first one was to illustrate the point
    Last edited by silentweed; 06-12-2006 at 07:38 AM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

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