Skip to main content
January 14, 2013
Answered

For Loop

  • January 14, 2013
  • 2 replies
  • 542 views

Hello Everybody

  This is kind of a follow up of my previous question regarding using loops to manipulate dots in a pattern. I came into a question. If I were to remove them as dots themselves and not as a whole, how do I do that? I believe that my main problem is the naming part. How do I name and remove them by their generated names? Thank you. Here is my code:

var Food:Array = new Array();


 

for(var f:uint = 1; f<=25; f++){

var foodRow1:food = new food();

foodRow1.name = "Row1"+f;

addChild(foodRow1);

foodRow1.x = arrayX[0];

foodRow1.y = f*25;

foodRow1.addEventListener(Event.ENTER_FRAME, checkCollision);

Food.push(foodRow1);

//Row 2

var foodRow2:food = new food();

foodRow2.name = "food_"+f;

addChild(foodRow2);

foodRow2.x = arrayX[1];

foodRow2.y = f*25;

//Row 3

var foodRow3:food = new food();

foodRow3.name = "food_"+f;

addChild(foodRow3);

foodRow3.x = arrayX[2];

foodRow3.y = f*25;

//Row 4

var foodRow4:food = new food();

foodRow4.name = "food_"+f;

addChild(foodRow4);

foodRow4.x = arrayX[3];

foodRow4.y = f*25;

//Row 5

var foodRow5:food = new food();

foodRow5.name = "food_"+f;

addChild(foodRow5);

foodRow5.x = arrayX[4];

foodRow5.y = f*25;

function checkCollision(event:Event){


trace("inside function");

}

}

for(var h:uint = 1; h<25;h++){


//Horz 1

var foodHorz1:food = new food();

foodHorz1.name = "food_"+h;

addChild(foodHorz1);

foodHorz1.x = h * 25;

foodHorz1.y = arrayY[0];



//Horz 2

var foodHorz2:food = new food();

foodHorz2.name = "food_"+h;

addChild(foodHorz2);

foodHorz2.x = h * 25;

foodHorz2.y = arrayY[1];



//Horz 3

var foodHorz3:food = new food();

foodHorz3.name = "food_"+h;

addChild(foodHorz3);

foodHorz3.x = h * 25;

foodHorz3.y = arrayY[2];



//Horz 4

var foodHorz4:food = new food();

foodHorz4.name = "food_"+h;

addChild(foodHorz4);

foodHorz4.x = h * 25;

foodHorz4.y = arrayY[3];



//Horz 5

var foodHorz5:food = new food();

foodHorz5.name = "food_"+h;

addChild(foodHorz5);

foodHorz5.x = h * 25;

foodHorz5.y = arrayY[4];



}

(it's based on a pacman game)

Thank you!!!

This topic has been closed for replies.
Correct answer kglad

you can use getChildByName() to obtain a reference and then use removeChild.  ie,

removeChild(getChildByName("food_1"));

but if you added all your items into an array (like Food), you could loop through your array to obtain references.

2 replies

sinious
Legend
January 14, 2013

The reason I put the code to push every added item into the array (and called it 'neat') is because it's an easy way to tidy up your dots. Just looping through the array you can remove all the items.

e.g.

while (MyFoodArray.length > 0)

{

     // remove any extra stuff (listeners, etc) from MyFoodArray[MyFoodArray.length - 1]

     // then get rid of item (garbage collection will remove deref'd item when needed)

     removeChild(MyFoodArray.pop());

}

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
January 14, 2013

you can use getChildByName() to obtain a reference and then use removeChild.  ie,

removeChild(getChildByName("food_1"));

but if you added all your items into an array (like Food), you could loop through your array to obtain references.