How to remove child in an array in actionscript - arrays

How to remove a child in an array in ActionScript 3.0?
Here's my code. I put the child I want to remove in an array called nailList[] and put it on gameStage.
function makeNails():void
{
for (var i=0; i< 3; i++)
{
for (var j=0; j<3; j++)
{
var tempNail = new Nail2(j * C.NAIL_DISTANCE + C.NAIL_START_X,
i * C.NAIL_DISTANCE + C.NAIL_START_Y);
nailList.push(tempNail);
gameStage.addChild(tempNail);
}
}
gameStage.addEventListener(Event.ENTER_FRAME,update);
gameStage.addEventListener(MouseEvent.CLICK, clickToHit);
}
Here's the code I used to remove the nails. But nothing happens after this code. The nails are still there.
for (var j:int = nailList.length; j >= 0; j--)
{
removeNails(j);
trace("Remove Nail");
}
function removeNails(idx:int)
{
gameStage.removeChild(nailList[idx]);
nailList.splice(idx,0);
}
I want to remove the MovieClip so that I can restart the game and add new ones.

Valid array index range is from 0 to length - 1. You are trying to call removeChild with nailList[nailList.length] which is invalid and removeChild should fire an error. You need to change the initial value of j to nailList.length - 1.
for (var j:int = nailList.length - 1; j >= 0; j--)
^^^
Another problem is (as pointed in the comment) the second parameter of splice is delete count. So you need to use 1 instead of 0.
nailList.splice(idx, 1);
^

You can use splice to remove the child of an array.
arrName.splice(2,1);
Hope it will help.

Related

Changing data in array with substr in loop

I have a list of words in myArray, I need to make them all have 12 symbols, no more, no less. So first of all I weed out all words shorter than 12 symbols. IT WORKS. But then I must to cut extra symbols in words longer than 12 symbols. I use array12[j].substr(0,12); and its not working.
My question is: How to change data in array "on the fly" - in a loop? Trace I use after substr returns me same array long words, without cut.
function myArrayLoopFunction()
{
for (var i:int = 0; i < myArray.length; i++) // this works fine
{
if (myArray[i].length >= 12) {
array12.push(myArray[i]);
}
}
for (var j:int = 0; j < array12.length; j++)
{
if(array12[j].length > 12 )
{
array12[j].substr(0,12); //doesnt work
trace(array12[j]);
}
}
}
You can do it in one loop. I think your mistake is that you're not assigning the result of the substr method back to your array:
function myArrayLoopFunction()
{
for (var i:int = 0, str:String; i < myArray.length; i++)
{
str = myArray[i];
if (str.length >= 12) {
myArray[i] = str.substr(0, 12);
}
}
}
You're not assigning the substring to anything.
array12[j].substr(0,12);
should be
array12[j] = array12[j].substr(0,12);

Adding a fixed number of unique items to an array using a for loop and indexOf

I'm using a for loop to get 6 random numbers between 1-20, using indexOf to omit duplicates, and pushing them to an array.
However, I always want 6 items in the array, so I'd like duplicates to be replaced. In my naive code duplicates are simply omitted, which means that sometimes I'll get less than 6 in the array. How do I replace the omissions to fill those 6 array slots?
function rolld(event:MouseEvent) {
for (i = 0; i < 6; i++){
d = (Math.floor(Math.random() * (1 + d_hi - d_lo)) + d_lo);
if (rollArray.indexOf(d) < 0){
rollArray.push(d);
}
}
trace (rollArray);
}
Still very new to this. Thanks for any help!
When you get an element from array with 20 elements, try to remove it from array.
function rolld(event:MouseEvent) {
var elements:Array = [];
for (var i:int = 1; i <= 20; i++)
{
elements.push(i);
}
for (i = 0; i < 6; i++){
d = (Math.floor(Math.random() * elements.length);
rollArray.push(d);
//remove the element
elements.splice(d, 1);
}
trace (rollArray);
}

Assigning instance names to array Objects: ActionScript 3.0

I'll just start by saying that I am a bit new to programming, and I apologize if this is a stupid question.
I have a timer running in my application that at every interval, creates an a new instance of a MovieClip called blueBall.Here is my code:
var randomX:Number = Math.random() * 350;
var newBlue:mc_BlueBall = new mc_BlueBall ;
newBlue.x = randomX;
newBlue.y = -20;
for(var i:Number = 0;i < blueArray.length; i++)
{
newBlue.name = "newBlue" + i;
}
blueArray.push(newBlue);
addChild(newBlue);
}
var randomX:Number = Math.random() * 350;
var newBlue:mc_BlueBall = new mc_BlueBall ;
newBlue.x = randomX;
newBlue.y = -20;
for(var i:Number = 0;i < blueArray.length; i++)
{
newBlue.name = "newBlue" + i;
}
blueArray.push(newBlue);
addChild(newBlue);
}
My question is: How do I make it so that each newly created object in the array has it's own hitTestObject Event? I want to make it so that if the if the user's icon touches one of the newBlue objects, that newBlue object with be removed, and the score will go up a point.
Thanks!
this is my first time answering a question here but I hope I can help! Assuming you have a timer for your main game loop, you should try something like this once per frame:
//For each blue ball in the array
for(var i:int = 0; i < blueArray.length; i++) {
//If it touches the player
if(blueArray[i].hitTestObject(thePlayerMC)) {
//Increment the score
score++;
//Remove from stage and array
removeChild(blueArray[i]);
blueArray.splice(i, 1); //<-At index i, remove 1 element
//Decrement i since we just pulled it out of the array and don't want to skip the next array item
i--;
}
}
This is sort of the quick and dirty solution, but highly effective and commonly used.

How could I write the function for the card game, War, cleaner?

I've been working on writing the card game war in Unity and somewere in the War function, I lose 2 cards. Is there any easier way of doing this, or could anyone let me know what I'm doing wrong?
while(Flipped1[Flipped1.length-1].CardValue == Flipped2[Flipped2.length-1].CardValue)
{
for(var i= 0; i < 3; i++)
{
//adds a card from the players hand to the flipped pile
Flipped1.Add(playerOneCards[0]);
Flipped2.Add(playerTwoCards[0]);
//removes card from the array.
playerOneCards.RemoveAt(0);
playerTwoCards.RemoveAt(0);
}
p1War = Flipped1[Flipped1.length-1].img;
p2War = Flipped2[Flipped2.length-1].img;
Debug.Log(Flipped1[i].CardValue);
Debug.Log(Flipped2[i].CardValue);
Debug.Log(Flipped1[0].CardValue);
Debug.Log(Flipped2[0].CardValue);
if(Flipped1[i].CardValue < Flipped2[i].CardValue)
{
for(var j =0; j < Flipped1.length +2 || j < Flipped2.length+2; j++)
{
playerTwoCards.Add(Flipped1[0]);
playerTwoCards.Add(Flipped2[0]);
//removes card from the array.
Flipped1.RemoveAt(0);
Flipped2.RemoveAt(0);
}
}
else
{
for(var q =0; q < Flipped1.length+2|| q < Flipped2.length+2; q++)
{
playerOneCards.Add(Flipped2[0]);
playerOneCards.Add(Flipped1[0]);
//removes card from the array.
Flipped1.RemoveAt(0);
Flipped2.RemoveAt(0);
}
}
}
Your problem is in the logic in your for loop (inside the if/else). You actually want to make that something like while (Flipped1.length > 0), since what you're trying to do is empty the Flipped arrays, rather than effectively comparing it to an incrementing var less a constant.

Push Objects with Dynamically Allocated Names into Array - AS3

Is this possible? For example, if I had 100 items named item1.....item100, could I add them all to an array using a loop? Something along these lines....but this doesn't work obviously:
for (var i:int = 1; i <= 100; i++)
{
myArray.push("label" + 1);
}
Luckily I only have 10 items, so I can do it manually, but I'm just curious for future reference...
Thanks!
The array access operator will let you reference properties and objects by name like that, so if you have:
myArray.push(label1);
You can use this instead:
myArray.push(this["label" + 1]);
Depends on on you mean by "items".
If they are properties of the current class use:
for (var i:int = 1; i <= 100; i++)
{
myArray.push(this["label" + i]);
}
You can do this even if the names of the objects do not follow any particular pattern but they have to be inside a 'container' (you will add all objects from the container to the array):
for (var i:int = 0; i < containerName.numChildren; i++)
{
myArray.push(containerName.getChildAt(i);
}
You have to get the link to the object instance to push it into the array, for example if the names of your text fields are label1, label2, etc. you can use the following code:
for (var i:int = 1; i <= 100; i++)
{
myArray.push(labelsContainer.getChildByName("label" + i));
}

Resources