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
Class:BallCode: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 } } } }
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 valuesCode: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 } } }
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)




)
Reply With Quote