A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [F9] for in loop

  1. #1
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139

    [F9] for in loop

    ok after reading a book on OOP Programming i have finally figured it out (well to an extent )

    my experiment now works, i have 200 clips moving on stage, hoorah but now i'm just wondering if i should worry about giving these clips their own events or should i do it the old fashion way and run a (for in loop). I originally tried this, but soon realised that my loop was not finding any instances and therefore couldn't control any of them. Has there been changes to the for in loop?

    here's my code, hopefully i've done everything right
    Class:Main - inherited from an instance on stage
    Code:
    package com.balls {
    	
    	//flash imports
    	import flash.display.Sprite
    	import flash.events.*
    	//ball class, used to make them balls
    	import com.balls.Ball
    	
    	public class Main extends Sprite {
    
    		public function Main():void{
    			var balls:Balls = new Balls()
    			addChild(balls)
    		}		
    	}
    	
    	class Balls extends Sprite {
    		
    		//define any variables
    		private var ballNum:Number = 200
    		
    		public function Balls():void{
    			//create balls
    			for(var i:Number=0;i<ballNum;i++){
    				var x:Number = Math.random()*400
    				var y:Number = Math.random()*400
    				var b:Ball = new Ball(x,y)
    				//add event listener to control all balls
    				addEventListener(Event.ENTER_FRAME, controlBalls);
    				addChild(b)
    			}
    		}
    		
    		private function controlBalls(e:Event){
    			//loop through all instances
    			for(var i:String in this){
    				var b:Sprite = this[i]
    				b.control()
    				trace(b) //traces nothing
    			}
    		}
    	}
    }
    Class:Ball
    Code:
    package com.balls {
    	
    	//flash imports
    	import flash.display.MovieClip	
    	
    	public class Ball extends MovieClip {
    		
    		//make reference to our brain for outside functions
    		public var control:Function
    		//our inital speeds
    		private var vx:Number = Math.random()*2
    		private var vy:Number = Math.random()*2
    		
    		
    		public function Ball(x:Number,y:Number):void{
    			this.x = x
    			this.y = y
    			control = moveBall
    		}
    		
    		private function moveBall():void{
    			x += vx
    			y += vy
    		}
    	}
    }
    also, am i referencing the instance correctly in the for in loop: var b:Sprite - and for some weird reason i could use uint, kept returning an error, i wanted to because its faster than Number and only uses positive values

    that doesn't work, but if i give each Ball its own event Listener it does. Maybe i could create an array and then loop through that array, but a for in loop should work (at least in flash8)
    Last edited by mr_malee; 07-06-2006 at 04:31 AM.
    lather yourself up with soap - soap arcade

  2. #2
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    The for...in loop probably hasn't changed (on a side note, there is supposed to be a new for each loop though), it's the way you access children that has changed. I don't think parentMC[childName] still works, but I haven't tried this. Maybe it works if you set your movieclip-extending-class to dynamic, but that's just a wild guess, and I doubt it.

    You can try to loop i from 0 to mc.numChildren, and use mc.getChildAt(i) - not sure if it will work, but it might.

    If you want to access a child by name, use mc.getChildByName(name) - however, that's no good for a for...in loop.
    Last edited by Fall_X; 07-06-2006 at 04:39 AM.

  3. #3
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    works a treat, i'm so used to accessing dynamic objects this with the array syntax:

    parent[child]

    guess i'll have to adapt

    cheers
    lather yourself up with soap - soap arcade

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