A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [F9] for in loop

Threaded View

  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

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