Adding a child to a child - actionscript-3

In AS3, I am trying to add a child to a specific child of a MovieClip.
To do this I have done the following:
for (var r:Number = 0; r < 2; r++) {
sand = new Sand();
sLayers.getChildAt(r).addChild(sand);
}
When sLayers is the MovieClip parent, r is the specific child of sLayers, and sand is the child I want to add.
It comes up with the following error:
1061: Call to a possibly undefined method addChild
through a reference with static type flash.display:DisplayObject.
Sorry I simplified the code, it would take up too much space.
Is there another way to do this? Or am I close but just missing something?

try this
for(var r:Number = 0; r < 2; r++)
{
var sand:Sand = new Sand;
(sLayers.getChildAt(r) as MovieClip).addChild(sand);
}

Related

remove life from stage after mouseclick as3 code is not working

i have added 5 life icons on a container in stage..now i want to remove them one by one by mouse click.it is working for first click(one child is removing from stage).but it is not working after that.what should i do.there is no error in code but not working.here is the code
var Lives:Number = 5;
var Spacing:Number = 5;
var nextX:Number = 0;
for(var i:int = 0; i < Lives; i++ )
{
var mc:MovieClip = new mcPlayerLives();
mc.x = nextX;
lives_container.addChild(mc );
nextX += mc.width + Spacing;
}
attackButton.addEventListener(MouseEvent.CLICK, removeLife);
function removeLife(event:MouseEvent):void
{
// Lives= Lives - 1;
if (lives_container.contains(mc))
lives_container.removeChild(mc);
}
You've defined the variable mc in the loop. When loop end, mc point to the last instance of the mcPlayerLives class. In the removeLife function you remove only last instance of the mcPlayerLives class.
Instead of
if (lives_container.contains(mc))
lives_container.removeChild(mc);
use
if (lives_container.numChildren)
lives_container.removeChildAt(0);
It means: if lives_container contains at least one child, remove the child.

Acessing "parent.child.MC"

I have a structure that is something like this - holder.lineX.lineoverlay
I want to access the movieclip "lineoverlay" on all children created
If I do something like this:
MovieClip(this.parent).lineX.lineoverlay.visible = false;
It works but I don't always know how many "line" movieclips I have, because they are created in runtime.
I tried this:
for (var i:uint = 1; i < MovieClip(parent).numChildren; i++) {
MovieClip(this.parent).getChildAt(i).lineoverlay.visible = false;
}
But I keep getting an error because flash treats "lineoverlay" as an undefined property and not as movieclip.
Can someone help?
getChildAt returns a DisplayObject. DisplayObject is not a dynamic object (unlike Movieclip). So just cast it to movieclip in that case.
This should work:
for (var i:uint = 1; i < MovieClip(parent).numChildren; i++) {
MovieClip(MovieClip(this.parent).getChildAt(i)).lineoverlay.visible = false;
}

as3 accessing Children from dynamicly added moiveclip

I am adding moiveClips and putting their position accordingly. I am placing all the dynamically added clips in one new movie clip and then centering that on stage. As a test, I see I can control the alpha in the main container but I can not access the children. How can I assess each flagButton separately? There would be several in this loop example below.
var flagButton:MovieClip;
var flagArray:Array = new Array;
var flagDisplayButtons:MovieClip = new MovieClip();
function displayFlagButtons()
{
for( var i = 0; flagArray.length > i; i++)
{
var flagButton:MovieClip = new roundButton();
flagButton.x = (flagButton.width + 10) * i;
flagButton.y = stage.stageHeight - flagButton.height;
addChild(flagButton);
flagDisplayButtons.addChild(flagButton);
}
addChild(flagDisplayButtons);
// Works
flagDisplayButtons.x = stage.stageWidth/2 - flagDisplayButtons.width/2;
// Does not see flagButton
flagDisplayButtons.flagButton.alpha = .5;
}
GETS ERROR:
TypeError: Error #1010: A term is undefined and has no properties.
In your example code, you have created a bunch of button instances, and one by one you have added them to the display list of flagDisplayButtons. They are on that display list in the order that you placed them.
first instance is at index 0
second instance is at index 1
third instance is at index 2
and so on.
So if you want to access the third button you added, you could do this :
var flagButton:RoundButton = flagDisplayButtons.getChildAt(2) as RoundButton;
Now that you have created variable to reference that button, you can do any of these :
flagButton.alpha = .5;
flagButton.x = 100;
flagButton.y = 200;
You need to use getChildAt or getChildByName to get the children of a DisplayObjectContainer
IE:
flagDisplayButtons.getChildAt(0).alpha = 0.5;

Trying to remove object throws error 1009 cannot access a property or method of null object reference

Working on a flash game, in a previous game I had enemies come in by using an array and when they were killed or moved off the stage I would just remove them from the array. For some reason when I use exactly the same code in this game, it throws a 1009: error when I try to remove the array object, basically saying that there's nothing there. . . Which is strange.
Here's the code:
public function addZombie()
{
var zom:Zombie = new Zombie();
zom.y = 20;
zom.x = Math.floor(Math.random()*(1 + 500 - 30)) + 30;
addChild(zom);
zombies.push(zom);
numZombies++;
}
That's the function where it's added in, zombies is the array and it's pushed into the array in this function. Here's the code where I'm attempting to remove it:
for (var i:int = 0; i < zombies.length; i++)
{
if (zombies[i].y + zombies[i].height / 2 > 400) {
removeChild(zombies[i]);
zombies.splice(i,1);
numZombies--;
addZombie();
}
}
removeChild(zombies[i]); <-- This is the part that throws the error when it attempts to remove it. It removes some of them strangely enough, but not all of them.
I don't believe the loop is doing quite what you expect it to. When you remove an element from an array in this way, all the elements after the removed elements are moved down. So, if you have code like:
var testArr:Array = new Array();
testArr.push('First');
testArr.push('Second');
testArr.push('Third');
testArr.push('Fourth');
for (var i:int = 0; i < testArr.length; i++) {
testArr.splice(i,1);
}
The results of the loop will be:
i=0, testArr = ['Second', 'Third', 'Fourth']
i=1, testArr = ['Second', 'Fourth']
i=2 END OF LOOP
It would probably be better to use a while loop, incrementing the index only if the element is not removed (since removing the element moves a new element to that index position).
I don't see why you're getting the null object reference, but this would be causing other problems (why some are removed but not all).
The explanations of Bill Turner are the correct ones, but as it didn't seem to help you fix your issue, you might want to try this:
var zombieCount:int = zombies.length;
for (var i:int = 0; i < zombies.length; i++)
{
var zombie = zombies[i];
if (zombie.y + (zombie.height / 2) > 400) {
removeChild(zombie);
zombies.splice(i,1);
i--; // So that the loop will pass with the same value next time
numZombies--;
}
}
for (var j:int = zombies.length; j < zombieCount; j++)
{
addZombie();
}

removing multiple movieclips

I'm developing a game.I've attached random movieClips from library. I've movieClips named picLeft1, picLeft2, picLeft3 and so on in library. Its working fine but i'm having problem removing it. the code below is for the attachment of movieclip. Here ranque is an array which stores randomly generated numbers up to 5 and HolderL is the movieClip in which I want to attach the movieClip. And q is a sprite.
for (var i:int = 0; int<3; i++) {
que_mc.push("picLeft"+ranque[i]);
var que_mc_class:Class = getDefinitionByName(que_mc[i]) as Class;
q = new que_mc_class();
this["HolderL"+(i+1)].addChild(q);
}
my code for removing the movieclip is given below.I want to remove all the movieclip attached in HolderL. but this code is not working.
for(var j:int = 1; j<=3; j++) {
this["HolderL"+j].removeChild(q);
}
Flash Player 11 allows you to use this:
for (var j:int = 1; j<=3; j++) {
var container : DisplayObjectContainer = this["HolderL"+j];
container.removeChildren();
}
Earlier versions don't have the removeChildren() command; you have to have two loops: One to loop to iterate over the container movie clips, one to traverse the display list and remove all children.
Shortest way to remove all children of a container prior to FlashPlayer 11:
while (container.numChildren > 0) container.removeChildAt(0);
It's not clear from your code what q is, but basically if you want to remove all the children from one movie clip, the simplest is to loop through all the children and remove them one by one. For example:
for (var i:int = container.numChildren - 1; i >= 0; i--) {
container.removeChildAt(i);
}