Copy link to clipboard
Copied
Hi All
//This small class add the same movieclip to the stage with random scale and position
// Garden represents a movieClip in the library
//attached fla and document class
My question :
Why using the same name garden for all instances of Garden is not a problem?
Can I track each individual instance of Garden on the screen or recall it for something else?
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class GardenClass extends MovieClip {
private var timer:Timer = new Timer(100);
public function GardenClass ():void
{init();}
public function init ():void
{ timer.addEventListener(TimerEvent.TIMER, makeGarden);
timer.start();
}
private function makeGarden(e:TimerEvent):void
{ var garden:Garden = new Garden();
garden.scaleX = garden.scaleY = Math.random();
garden.x = Math.random()*this.stage.stageWidth;
garden.y = Math.random()*this.stage.stageHeight;
this.addChild(garden);
}
}
}
This is because instance is create in the scope of the function. Compiler, perhaps, assign unique pointers to each instance.
In order to track them you can take several approaches.
On can be to give an instance unique name. In your case because there is only one instance of garden per instance of GardenClass - you may need to give name to the instances of GardenClass:
var gardenInst0:GardenClass = new GardenClass();
gardenInst0.name = "garden_0";
var gardenInst1:GardenClass = new GardenClass();
garden
...Copy link to clipboard
Copied
This is because instance is create in the scope of the function. Compiler, perhaps, assign unique pointers to each instance.
In order to track them you can take several approaches.
On can be to give an instance unique name. In your case because there is only one instance of garden per instance of GardenClass - you may need to give name to the instances of GardenClass:
var gardenInst0:GardenClass = new GardenClass();
gardenInst0.name = "garden_0";
var gardenInst1:GardenClass = new GardenClass();
gardenInst1.name = "garden_1";
var gardenInst2:GardenClass = new GardenClass();
gardenInst2.name = "garden_2";
then you can read them like:
trace(gardenInst2.name);
Or:
trace(this["garden2"]);
If you want to get to garden instance inside an instance of GardenClass you need to make it accessible/public:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class GardenClass extends MovieClip {
private var timer:Timer = new Timer(100);
// declare garden in the scope of class
private var garden:Garden;
public function GardenClass ():void
{init();}
public function init ():void
{ timer.addEventListener(TimerEvent.TIMER, makeGarden);
timer.start();
}
private function makeGarden(e:TimerEvent):void
{
garden = new Garden();
garden.scaleX = garden.scaleY = Math.random();
garden.x = Math.random()*this.stage.stageWidth;
garden.y = Math.random()*this.stage.stageHeight;
this.addChild(garden);
}
// this method allows to access the garden instance
public function getGarden():Garden{
return garden;
}
}
}