For loop help

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.


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 );
            }
            
        }
        
    }
}

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:

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:

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.

duh, it’s pretty obvious now
thanks so much

Glad to help :hoser: