GregorII
GregorII

Reputation: 221

addChild empty when it should not be

Currently im trying to make a moving background.So im calculating how many objects can be on the stage and then adding those objects to 2 containers but it seems that it is just adding them to the last container spriteContainer2 when i trace with ....numChildren

so i know what is wrong but .. i dont know why it is doing so. And can someone help me with a solution for this, because im not that familiar with all the methods in flash.(in real world i would get the 1st container and make a copy of it and then use it )

    var speed:int = 1
var offset:int;
var spriteContainer:Sprite = new Sprite;
var spriteContainer2:Sprite = new Sprite;

var redSqBitmapData:BitmapData = new Tile_1();
var redSqBitmapData2:BitmapData = new Tile_2();
var my_shape:Bitmap = new Bitmap(redSqBitmapData);

my_shape.scaleX  = my_shape.scaleY = stage.stageWidth / my_shape.width
trace(my_shape.height)

var stageH:Number = stage.stageHeight;
var elementH:Number = my_shape.height;
var numberElements:int = Math.ceil(stageH / elementH);
var i:int=0

for( i; i< numberElements; i++){

    var my_shape2 = new Bitmap(redSqBitmapData);
    my_shape2.scaleX  = my_shape2.scaleY = stage.stageWidth / my_shape2.width
    my_shape2.x = 0;
    my_shape2.y = my_shape.height * i
    spriteContainer.addChild(my_shape2)
    spriteContainer2.addChild(my_shape2)

}

addChild(spriteContainer)
trace(spriteContainer.numChildren)
addChild(spriteContainer2)
trace(spriteContainer2.numChildren)

this.addEventListener(Event.ENTER_FRAME, onEveryFrame)
function onEveryFrame(evt:Event):void{

    offset +=  speed;
    spriteContainer.y = offset % spriteContainer.height - spriteContainer.height;
    spriteContainer2.y = spriteContainer.y - spriteContainer2.height

}

Upvotes: 1

Views: 78

Answers (1)

Azzy Elvul
Azzy Elvul

Reputation: 1438

Instance of DisplayObject can be only in only one DisplayObjectContainer. Adding the same DisplayObject into new DisplayObjectContainer will remove it from previous Container. Check this: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#addChild()

"...If you add a child object that already has a different display object container as a parent, the object is removed from the child list of the other display object container.."

Upvotes: 1

Related Questions