A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: problem in simple script

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    7

    Unhappy problem in simple script

    I was trying to make snow in flash, when problem appeared. Here's main .fla file AS:
    Code:
    import fl.controls.Slider;
    import fl.controls.SliderDirection;
    import fl.controls.CheckBox;
    
    
    import bell;
    var mc:MovieClip = new MovieClip();
    addChild(mc);
    var windVal:Number = 0;
    var frq:Number = 1; 
    var dados:Boolean = false;
    
    stage.addEventListener(Event.ENTER_FRAME, snow);
    wind_sldr.addEventListener(Event.CHANGE, wv);
    herz.addEventListener(Event.CHANGE, ww);
    cbb.addEventListener(MouseEvent.CLICK, cx);
    
    function cx(e:MouseEvent)	{
    	dados = cbb.selected;
    	if(!dados)	{
    		for(var p:Number = 0; p<mc.numChildren; p++)	{
    			//if(Object(mc.getChildAt(p)).devs)	{
    				Object(mc.getChildAt(p)).r();
    				mc.removeChildAt(p);
    			//}
    			
    		}
    		
    	}
    	
    }
    
    function wv(e:Event)	{
    windVal = wind_sldr.value;	
    }
    
    function ww(e:Event)	{
    	frq = herz.value;
    }
    
    
    function snow(e:Event)	{
    	for(var l:Number = 0; l<frq; l++)	{
    	var m:bell = new bell();
    	
    	mc.addChild(m);
    	}
    	
    	
    }
    And bell.as:
    Code:
    package {
    
    	import flash.display.Sprite;
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	import fl.transitions.Tween;
    	import fl.transitions.TweenEvent;
    	import fl.transitions.easing.*;
    
    	public class bell extends Sprite {
    
    		public var spd:Number;
    		public var ws:Number;
    		public var wina:Number;
    		public var devs:Boolean;
    
    		public function bell() {
    			addEventListener(Event.ADDED_TO_STAGE, added);
    			addEventListener(Event.ENTER_FRAME, ef);
    
    			this.graphics.beginFill(0xFFE9F7);
    			this.graphics.drawCircle(0, 0, Math.random()*5);
    			this.graphics.endFill();
    		}
    
    		public function added(e:Event) {
    			this.x = Math.random() * MovieClip(root).stage.stageWidth;
    			spd = 1 + Math.random() * 4;
    		}
    
    
    		public function ef(e:Event) {
    			
    			
    			
    			this.y += spd;
    			if (MovieClip(root).windVal!=0) {
    				if (MovieClip(root).windVal != wina) {
    					ws = MovieClip(root).windVal - MovieClip(root).windVal/2 + Math.random()*MovieClip(root).windVal/2;
    					wina = MovieClip(root).windVal;
    				}
    				this.x+=ws;
    
    			}
    
    			if (this.y >= MovieClip(root).stage.stageHeight) {
    				removeEventListener(Event.ENTER_FRAME, ef);
    				devs = true;
    				if(!MovieClip(root).dados)	{
    				MovieClip(root).mc.removeChild(this);
    				}
    				
    			}
    		}
    
    
    
    		public function r()	{
    			removeEventListener(Event.ENTER_FRAME, ef);
    		}
    
    
    	}
    
    }
    when cx() function is run, not every child of mc MovieClip is removed. Why?

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Your loop is incrementing at the same time that the display list is shrinking - so if you had 6 items in your display:

    1, 2, 3, 4, 5, 6

    You go through the loop with p=0 and delete #1,

    __ 2, 3, 4, 5, 6

    Then increment p and delete index 2:

    __ 2, __ 4, 5, 6

    Then increment p and delete index 3:

    __ 2, __ 4, __ 6

    Now p is incremented to 4 and numChildren is 3 so the loop ends. Here's a loop that will remove everything:

    PHP Code:
    while(mc.numChildren){ removeChildAt(0) }; 
    Please use [php] or [code] tags, and mark your threads resolved 8)

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