A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Array/loop not functioning as expected

  1. #1

    Array/loop not functioning as expected

    I have a series of MC's (ultimately about 55) which I'm performing some actions on when a user hovers over the MC.
    Code:
    var items:Array = [
    			UnitedStates,
    			Brazil,
    			China
                            //and so on...
    			];
    
    var arrayLen:Number = items.length;
    trace(arrayLen); //make sure our count is correct
    
    for (var i=0; i<arrayLen; i++) {
    	trace(i); //make sure we're iterating 
    	var MC = _level0["item_"+i];
    	trace(MC); //make sure our paths are correct
    
    	MC.label._visible = false;
    	MC.button.onRollOver = function() {
    		MC.label._visible = true;
    		MC.label._alpha = 0;
    		var myTween:Tween= new Tween(MC.label, "_alpha", mx.transitions.easing.Strong.easeOut, 0, 100, 1, true);
    		var myTween:Tween= new Tween(MC.button, "_alpha", mx.transitions.easing.Strong.easeOut, 50, 100, 1, true);
    	};
    	MC.button.onRollOut = function() {
    		var myTween:Tween= new Tween(MC.label, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 0, 1, true);
    		var myTween:Tween= new Tween(MC.button, "_alpha", mx.transitions.easing.Strong.easeOut, 100, 50, 1, true);
    		if(MC.label._alpha == 0){
    			MC.label._visible = false;
    		}
    	};
    	stop();
    
    }
    All my variables and paths test correctly; but, when I test the swf, if I hover over _level0.item_0, _level0.item_2 is the MC that received the actions. As I add more MC's, it's always the last MC that receives the behavior. To clarify though, the _visibility and _alpha values do set themselves for all of the MC's. It's just when a user hovers over _0 and _1 that nothing happens to the clip being hovered and _2 receives the order to execute the tween.

    It's been awhile since I've worked in FLASH abd I'm sure I'm missing something here.

    Hopefully someone can point it out to me.

    (sorry...forgot...using FLASH CS4 AS2)

    Thanks
    Last edited by hothousegraphix; 10-08-2009 at 03:55 PM. Reason: provide flash version

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    loops only update the stage after the last iteration, hence only the last is active.
    move the button settings out of the loop -
    PHP Code:
    import mx.transitions.Tween;

    var 
    items:Array = [UnitedStates,Brazil,China];

    var 
    arrayLen:Number items.length;

    for (var 
    i=0i<arrayLeni++) {
        var 
    MC _level0["item_"+i];
        
    MC.label._visible false;
        
    buttons(MC)
    }

    function 
    buttons(MC){
    trace(MC);
    MC.button.onRollOver = function() {
            
    MC.label._visible true;
            
    MC.label._alpha 0;
            var 
    myTween:Tween= new Tween(MC.label"_alpha"mx.transitions.easing.Strong.easeOut01001true);
            var 
    myTween:Tween= new Tween(MC.button"_alpha"mx.transitions.easing.Strong.easeOut501001true);
        };
        
    MC.button.onRollOut = function() {
            var 
    myTween:Tween= new Tween(MC.label"_alpha"mx.transitions.easing.Strong.easeOut10001true);
            var 
    myTween:Tween= new Tween(MC.button"_alpha"mx.transitions.easing.Strong.easeOut100501true);
            if(
    MC.label._alpha == 0){
                
    MC.label._visible false;
            }
        };
    };

    stop(); 

  3. #3
    Got it, thanks much.

  4. #4
    Quick question, could this be done in a way so I don't have to rely on different "instance names" for each MC? The idea being that one single (the same) name is provided to each MC essentially treating it as a "class". If so, how? (this is where my programming knowledge admittedly lacks)

    Though this approach works, I can already foresee trouble maintaining this. The array data will ultimately be imported via XML which will be controlled via a CMS. I can imagine things getting screwy if orders are re-arranged.

  5. #5
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    it might pay you to google - *flash as2 movieclip class

  6. #6
    Thanks, I took a look and I think the approach would work but after thinking about it I've decided to go the more specific rout rather than taking a more general approach.

    Now I'm providing instance names for each MC based on the country names imported into my "items" array.

    Code:
    var items:Array = [
        UnitedStates,
        Brazil,
        Haiti,
        China
    ];
    
    var arrayLen:Number = items.length;
    
    for (var i=0; i<arrayLen; i++) {
        var MC:MovieClip = MovieClip(items[i]);
        MC.index = i;
        MC.label._visible = false;
        buttons(MC);
    }
    I think this will address my initial concerns as well as keep the FLA manageable.

    Thanks for the assistance, I appreciate it!

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