I am trying to make it so that when an instance of Instance1 is created it creates an instance of Instance2 with the same coordinates as it does, and has the instance of Instance2 follow it. Here is the code that I put in Instance1:
package {
import flash.display.MovieClip;
import flash.events.*;
public class Instance1 extends MovieClip {
private var _root:Object;
private var newInstance2:Instance2=new Instance2();
public function Instance1() {
addEventListener(Event.ADDED, beginClass);
}
private function beginClass(event:Event):void {
addEventListener(Event.ENTER_FRAME, eFrame);
_root=MovieClip(root);
_root.numInstance2++;
newInstance2=new Instance2();
newInstance2.name="i"+_root.numInstance2;
_root.addChild(newInstance2);
}
private function eFrame(event:Event):void {
for (var i:int=1; i<=_root.numInstance2; i++) {
if (_root.numInstance2!=0&&_root.getChildByName("i"+i)!=null) {
_root.getChildByName("i"+i).x=x;
_root.getChildByName("i"+i).y=y;
}
}
}
}
}
On the Maintimeline is this code:
var counter:int=0;
var numInstance2:int=0;
var newInstance1:Instance1=new Instance1();
addEventListener(Event.ENTER_FRAME, eFrame);
function eFrame(event:Event):void {
counter++;
if (counter>=100) {
newInstance1=new Instance1();
addChild(newInstance1);
counter=0;
}
}
But now it just creates a new instance of the class Instance2 every time the instance of the class Instance1 is created (so far so good), but it also takes all the other instances of the class Instance2 and repositions them to the coordinates of the newest-created Instance1. I would REALLY appreciate any help. I’ve been trying to do this for a long time and still cannot get it. Thank you very much in advance.