To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > General Help > Games

Reply
 
Thread Tools Search this Thread Display Modes
Old 07-06-2006, 05:17 AM   #1
mr_malee
M.D.
 
mr_malee's Avatar
 
Join Date: Dec 2002
Location: Shelter
Posts: 4,039
[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)
__________________
lather yourself up with soap - soap arcade

Last edited by mr_malee; 07-06-2006 at 05:31 AM.
mr_malee is offline   Reply With Quote
Old 07-06-2006, 05:28 AM   #2
Fall_X
crossconscious
 
Join Date: Sep 2005
Location: Belgium
Posts: 1,186
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 05:39 AM.
Fall_X is offline   Reply With Quote
Old 07-06-2006, 05:38 AM   #3
mr_malee
M.D.
 
mr_malee's Avatar
 
Join Date: Dec 2002
Location: Shelter
Posts: 4,039
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
mr_malee is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > General Help > Games

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:05 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.