A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Simple Array Code not working (AS2.0)

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    7

    Red face Simple Array Code not working (AS2.0)

    Hi, i'm trying to check through an array to see if any of my movie clips are currently on frame 15, before taking giant leaps I was trying to just get it to work with one var in the array before all of them, for some reason this code is not working, the trace is not coming up and I cannot figure out why not.

    Code:
    stop();
    
    var anybtn:Array = [btn1_mc, btn2_mc];
    trace(anybtn[1]);
    
    btn1.onPress = function(){
    	
    	if(anybtn[1]._currentframe == 15){
    		trace ("Array Check Working");
    	}
    	
    	btn1_mc.play();
    }
    
    btn2.onPress = function(){
    	
    	btn2_mc.play();
    }
    When the button first plays it stops on frame 15, my trace works fine if i use the movie clip directly like.

    Code:
    btn1.onPress = function(){
    	
    	if(btn1_mc._currentframe == 15){
    		trace ("Array Check Working");
    	}
    	
    	btn1_mc.play();
    }
    If someone would like to go a step further and show me how to check through all movie clips i would appreciate that as well. There will be many more than btn1_mc and btn2_mc so I will definitely need to use an array.

    Thanks!

  2. #2
    Flash Designer
    Join Date
    Oct 2008
    Posts
    58
    How about:

    stop();

    var anybtn:Array = [btn1_mc, btn2_mc];

    function btnPressed(){
    var _arrayLength:Number = anybtn.length();
    for(var i=0;i<_arrayLength;i++){
    if(anybtn[i]._currentframe == 15){
    trace(anybtn[i]+" is on frame 15");
    }
    }
    }

    function addListeners(){
    var _arrayLength:Number = anybtn.length();
    for(var i=0;i<_arrayLength;i++){
    var _btn = anybtn[i];
    _btn.onPress = btnPressed;
    }
    }
    addListeners();
    Last edited by LGLab; 08-23-2010 at 04:47 AM.

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Hey LG thanks so much for your reply! I didn't fully understand your code with the listener and I was getting an error of "calling a function in a nonfunciton" when checking anybtn.length. However thanks to realizing I just needed to add in a for loop fixed my problems!

    Here is my new code just in case anyone else needs it in the future

    Code:
    var anybtn:Array = [btn1_mc, btn2_mc, btn3_mc, btn4_mc, btn5_mc];
    
    btn1.onPress = function(){
    	for(var i=0;i<anybtn.length;i++){
    		if(anybtn[i]._currentframe == 15){
    			anybtn[i].play();
    		}
    	}
    	btn1_mc.play();
    }
    
    btn2.onPress = function(){
    	for(var i=0;i<anybtn.length;i++){
    		if(anybtn[i]._currentframe == 15){
    			anybtn[i].play();
    		}
    	}
    	btn2_mc.play();
    }
    This is fully working for me and has solved my problems altogether! Thanks again LG.

  4. #4
    Flash Designer
    Join Date
    Oct 2008
    Posts
    58
    Cool. Yeah you got that error because I put brackets after "length" in:

    var _arrayLength:Number = anybtn.length();
    it should have been:

    var _arrayLength:Number = anybtn.length;
    There are brackets in AS3 so got confused.

    I did that "_arrayLength" var because it is best practise in AS, when doing a for loop using an array's length, to first calculate the array's length and store it in a var, as in:

    function btnPressed(){

    //store array length in var
    var _arrayLength:Number = anybtn.length;

    //use the var in the for loop
    for(var i=0;i<_arrayLength;i++){
    if(anybtn[i]._currentframe == 15){
    trace(anybtn[i]+" is on frame 15");
    }
    }
    }
    This way, the array length is only computed once, while using "anybtn.length" directly into the loop function will force the function to re-calculate the array's length every time...more resources ;-)

    Glad you got it sorted ;-)
    Last edited by LGLab; 08-23-2010 at 01:17 PM.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Great info! I'll be sure to fix that now that I know why the code wasn't working!

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