# For loop help

**URL:** <https://forum.kirupa.com/t/for-loop-help/263766>\
**Category:** flash\
**Created:** [June 19, 2008, 1:29am UTC](https://forum.kirupa.com/t/for-loop-help/263766 "2008-06-19T01:29:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin416](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kevin416](https://forum.kirupa.com/u/kevin416)\
**Post date:** [June 19, 2008, 1:29am UTC](https://forum.kirupa.com/t/for-loop-help/263766/1 "2008-06-19T01:29:25Z")

</div>

Hi guys,  
The following for loop keeps getting stuck whenever I try to run this test project I made. It seems like the fix should be fairly straightforward, but I’m a bit new to as3. Thanks for any and all help.

```auto

package 
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    
    import flash.text.TextField;
    
    import flash.geom.Point;
    
    public class Main extends Sprite {
        
        
        public function Main():void {
            
            var num:int = 10;
            
            var container:MovieClip = new MovieClip()
            var points:Array = new Array( num );
            var pt:Point;
            var i:int = 0
            addChild( container );
            
            for ( i; i < points.length; i++ ) {
                
                pt = new Point( Math.random() * stage.width, Math.random() * stage.height ); 
                points.push( pt );
            }
            
        }
        
    }
}

```

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 19, 2008, 2:07am UTC](https://forum.kirupa.com/t/for-loop-help/263766/2 "2008-06-19T02:07:35Z")

</div>

It’s happens because each time you push a new index into the points array, it’s length increases so the condition of the loop is never reached. I’d reccomend forgoing setting the length of the array in it’s constructor and just using the num variable as the condition of the loop:

```auto
package 
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    
    import flash.text.TextField;
    
    import flash.geom.Point;
    
    public class Main extends Sprite {
        
        
        public function Main():void {
            
            var num:int = 10;
            
            var container:MovieClip = new MovieClip()
            var points:Array = new Array();
            var pt:Point;
            var i:int = 0
            addChild( container );
            
            for ( i; i < num; i++ ) {
                
                pt = new Point( Math.random() * stage.width, Math.random() * stage.height ); 
                points.push( pt );
            }
        }
        
    }
}

```

But you could also do it like this if you wanted to:

```auto
package 
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    
    import flash.text.TextField;
    
    import flash.geom.Point;
    
    public class Main extends Sprite {
        
        
        public function Main():void {
            
            var num:int = 10;
            
            var container:MovieClip = new MovieClip()
            var points:Array = new Array( num );
            var pt:Point;
            var i:int = 0
            addChild( container );
            
            for ( i; i < points.length; i++ ) {
                
                pt = new Point( Math.random() * stage.width, Math.random() * stage.height ); 
                points* = pt;
            }
        }
        
    }
}

```

And, fyi, to get the width and height of the actual Flash movie and not just it’s contents you’ll want to use the Stage.stageWidth and Stage.stageHeight properties.

---

<div class="post-metadata">

**Author:** ![kevin416](https://avatars.discourse-cdn.com/v4/letter/k/51bf81/32.png) [@kevin416](https://forum.kirupa.com/u/kevin416)\
**Post date:** [June 19, 2008, 2:13am UTC](https://forum.kirupa.com/t/for-loop-help/263766/3 "2008-06-19T02:13:11Z")

</div>

duh, it’s pretty obvious now  
thanks so much

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 19, 2008, 2:23am UTC](https://forum.kirupa.com/t/for-loop-help/263766/4 "2008-06-19T02:23:56Z")

</div>

Glad to help :hoser:
