A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: how to make a for loop

  1. #1
    Senior Member
    Join Date
    May 2016
    Posts
    269

    how to make a for loop

    How can I make a loop to shorten this code

    PHP Code:
    on (release) {
         
         
    p2=50
         p3
    =100
         p4
    =150
         p5
    =200
         p6
    =250
                  
            this
    .mover_mc.onEnterFrame=function(){
                
            
    image_2._x += ((p2-image_2._x)/(0.9*10)); 
            
    image_3._x += ((p3-image_3._x)/(0.9*10));
            
    image_4._x += ((p4-image_4._x)/(0.9*10)); 
            
    image_5._x += ((p5-image_5._x)/(0.9*10)); 
            
    image_6._x += ((p6-image_6._x)/(0.9*10));
                                    
            if ((
    Math.abs(image_2._x p2) < )){ 
                
    image_2._x p2;     
                
                }
    //--------------------------         
                                          
            
    else if ((Math.abs(image_3._x p3) < )){ 
                
    image_3._x p3;     
                
                }
    //-------------------------            
                                       
            
    else if((Math.abs(image_4._x p4) < )){ 
                
    image_4._x p4;     
                
                }
    //------------------------
                                   
            
    else if ((Math.abs(image_5._x p5) < )){ 
                
    image_5._x p5;     
                
                }
    //------------------------
                                         
            
    else if ((Math.abs(image_6._x p6) < )){ 
                
    image_6._x p6;     
               
                }
                
    delete this.mover_mc.onEnterFrame;  
                
    this.mover_mc.removeMovieClip();
    //-----------------------   
             
    }     


  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,237
    PHP Code:
    on (release){
    num=50
         
    this
    .mover_mc.onEnterFrame=function(){
    for(var 
    i=2;i<6;i++){
    this._parent["p"+i]=num*i
    this
    ._parent["image_"+i]._x+= ((this._parent["p"+i]- this._parent["image_"+i]._x)/(0.9*10));
    if ((
    Math.abs(this._parent["image_"+i]._x this._parent["p"+i]) < )){ 
    this._parent["image_"+i]._x this._parent["p"+i];      
    }
    }
       
    delete this.mover_mc.onEnterFrame;  
    this.mover_mc.removeMovieClip();
    }     

    Last edited by AS3.0; 01-28-2022 at 04:55 AM.

  3. #3
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,830
    The simplest would be to us a 2d array like this:
    Code:
    on (release) {
    	var dVar = [[_root.image_2, 50],[_root.image_3,100],[_root.image_4,150],[_root.image_5,200],[_root.image_6,250]];
    	this.mover_mc.onEnterFrame = function() {
    		for(var i=0;i<dVar.length;i++){
    			dVar[i][0]._x += ((dVar[i][1] - dVar[i][0]._x) / (0.9 * 10));
    			if ((Math.abs(dVar[i][0]._x - dVar[i][1]) < 1)) {
    				dVar[i][0]._x = dVar[i][1];
    			}
    		}
    		delete this.mover_mc.onEnterFrame;
    		this.mover_mc.removeMovieClip();
    	};
    }
    I haven't fully tested it but the array should work just fine.
    Edit: Didn't notice the previous post. Either way will work fine.
    .

  4. #4
    Senior Member
    Join Date
    May 2016
    Posts
    269
    thanks AS3.0 and swak
    it works fine

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