Push Objects with Dynamically Allocated Names into Array - AS3 - arrays

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));
}

Related

Iterating through objects with numbered names

I'm actually trying to figured out how i can iterate through some objects in a for loop with names like "Object1, Object2, Object3..."
Here is a code to exemplify what i'm trying to do:
for(int i = 0; i <= numberOfObjects; i++) {
someVariable = Object1.value/Object2.value/Object3.value;
}
In this case in the part of the code that i have the Object1,Object2 and Object3 i will change for something, example Object[i].
In this case it can't be done with arrays since i don't know how much objects were created.
For the C++ language
Put the objects into a std::vector.
Your loop will look like:
for (unsigned int i = 0; i < object_vector.size(); ++i)
{
result = result / object_vector[i].value;
}
For the C language
object my_objects[] = {object1, object2, /*...*/};
array_size = sizeof(my_objects) / sizeof(object);
for (unsigned int i = 0; i < array_size; ++i)
{
result = result / my_objects[i].value;
}
Use an array:
std::vector<T> objects{Object1, Object2, Object3};
for (auto&& x : objects) {
someVariable = x.value;
}
There's no other way to do it.

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);
}

How to remove child in an array in actionscript

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.

Two Dimension Array with Custom Type Elements

I'm trying to create on my scene an n x n size matrix, each element should be a Movie Clip, named Table, already prepared in the Library.
var tables:Array.<Table> = new Array.<Table>(tablesDimension, tablesDimension);
for (var i:int = 0; i < tablesDimension; i++) {
for (var j:int = 0; j < tablesDimension; j++) {
var tempTable:Table = new Table();
tempTable.x = i * 150 + 100;
tempTable.y = j * 100 + 100;
stage.addChild(tempTable);
tables.push(tempTable);
trace(tables[0][0].x);
}
}
A Movie Clip (in this case, Table) cannot be put in two dimensional arrays? Should I use some type conversion in my last line, at the trace(tables[0][0].x); to suggest to the compiler: it's about a Table type object?
The error message I receive: "Type parameters with non-parameterized type"
I'm not sure why you're trying to do with your first line but it's incorrect.
To quote the adobe actionscript reference:
The Array() constructor function can be used in three ways.
See this adobe reference link on Array creation. Or you can use Vectors (which are typed, like you seem to want to have).
So basically you want to create an Array, that will itself contain arrays. You need to create the arrays contained in it when you go through the first one. Else you will try to push into non existing elements. Also, you need to add the index as RST said.
var tables:Array.<Table> = new Array();
for (var i:int = 0; i < tablesDimension; i++) {
tables[i] = new Array();
for (var j:int = 0; j < tablesDimension; j++) {
var tempTable:Table = new Table();
tempTable.x = i * 150 + 100;
tempTable.y = j * 100 + 100;
stage.addChild(tempTable);
tables[i].push(tempTable);
}
}
trace(tables[0][0].x);
This should work.
I think you are missing an index. Try this
for (var i:int = 0; i < tablesDimension; i++) {
for (var j:int = 0; j < tablesDimension; j++) {
var tempTable:Table = new Table();
tempTable.x = i * 150 + 100;
tempTable.y = j * 100 + 100;
stage.addChild(tempTable);
tables[i].push(tempTable);
trace(tables[0][0].x);
}
}

as3 using addChild with an array containing indexes

so i have an array containing many instances. let's say movieclips.
and i have another array which contains numbers..in this case those numbers represent selected indices that i've somehow chosen!
var manydots:Array = new Array ();
for (var i=0; i<10; i++)
{
var newDot:dot = new dot ;
manydots.push(newDot);
}
var indices:Array = [0,1,5,8,4]
i want to use AddChild to add those movieclips into my scene, but not all of them, only selected indices contained in my 2nd array
I think this is what you are looking for,
for (var j=0; j<indicies.length; j++) {
addChild(manyDots[incidies[j]]);
}
sberry solution is correct. But you may also want to check that you actually are not adding null as a child.
for each(var i:int in indices) {
if (i < manydots.length) {
var d:dot = manydots[i];
if (d) {
addChild(d);
}
}
}

Resources