Reputation: 221
How do i add another object like this one without doing all the code again and without displaying it in 2 separate MovieClip Containers?
i have this BG is a simple square 10px x 10px
private var bg:BG = new BG();
private var myContainer:MovieClip = new MovieClip();
public function MainClass_Test() {
createRow_Col();
}
private function createRow_Col() {
var bg:MovieClip = new BG();
for (var i:int = 0; i < 5; i++) {
bg[i] = new BG();
for (var j:int = 0; j < 5; j++) {
bg[i][j] = new BG();
myContainer.addChild(bg[i][j]);
bg[i][j].x = bg[i][j].width * j + j * 10;
bg[i][j].y = bg[i][j].height*i + i*10
;
}
}
myContainer.x = stage.x
myContainer.y = stage.stageHeight / 2 - myContainer.height / 2;
addChildAt(myContainer,0);
}
how do i make a myContainer2 with the same movie clips as the 1st one ,so then i can add it next to the 1st one
myContainer2.x = myContainer.x + myContainer.width +10
myContainer2.y = stage.stageHeight / 2 - myContainer2.height / 2;
Upvotes: 0
Views: 53
Reputation: 13532
A simple way is to change createRow_Col
to take in a container and an x,y
:
private var bg:BG = new BG(); // this is not used as another `bg` is declared in side the function
private var myContainer:MovieClip = new MovieClip();
private var myContainer2:MovieClip = new MovieClip();
public function MainClass_Test() {
createRow_Col(myContainer, stage.x, stage.stageHeight/2 - myContainer.height/2);
createRow_col(myContainer2, myContainer.x + myContainer.width + 10);
}
private function createRow_Col(container:MovieClip, x:Number, y:Number) {
var bg:MovieClip = new BG();
for (var i:int = 0; i < 5; i++) {
bg[i] = new BG();
for (var j:int = 0; j < 5; j++) {
bg[i][j] = new BG();
container.addChild(bg[i][j]);
bg[i][j].x = bg[i][j].width * j + j * 10;
bg[i][j].y = bg[i][j].height * i + i * 10;
}
}
container.x = x;
container.y = y;
addChildAt(container, 0);
}
A better way would be to wrap the functionality in createRow_Col in a class that you derive from MovieClip so you only instantiate 2 instances and add to stage.
Upvotes: 2
Reputation: 64
I would make it like this. Create Background class and put
private function createRow_Col()
as part of Background.as and call that function when bg is added to stage using
addEventListener(Event.ADDED_TO_STAGE, onAdded) // in Background.as
Then in main class make vars bg1 and bg2, and add the to your container. I would guess that you whant this so you can make some transition effect, if thats not the case, you can just leave it as is and put j < 10 instead of j < 5 in loop.
Hope this will help!
Upvotes: 0
Reputation: 4665
Why don't you just create a class for the container? You can then instantiate it as many times as you want with the same properties...
Upvotes: 0