Is there any way to create instances of array in a for loop?
Here's my code...
var recArrCon1:Array = new Array(50);
var recArrCon2:Array = new Array(50);
var recArrCon3:Array = new Array(50);
var recArrCon4:Array = new Array(50);
var recArrCon5:Array = new Array(50);
var recArrCon6:Array = new Array(50);
var recArrCon7:Array = new Array(50);
var recArrCon8:Array = new Array(50);
I want to make declaration in a dynamic way by a for loop.
Thanks in advance.
By the way, I'm using AS3
Edit: The answer is (from Barış Uşaklı):
var recArrCons:Object = {};
for(var i:int=1; i<=8; i++)
{
recArrCons["recArrCon" + i] = new Array(50);
}
trace(recArrCons.recArrCon4); // 4th array
Make the class containing this code dynamic then you can create the names dynamically.
for(var i:int=1; i<=8; i++)
{
this["recArrCon" + i] = new Array(50);
}
trace(this.recArrCon4); // 4th array
Or you can store them in an Object like :
var recArrCons:Object = {};
for(var i:int=1; i<=8; i++)
{
recArrCons["recArrCon" + i] = new Array(50);
}
trace(recArrCons.recArrCon4); // 4th array
I wouldn't create instances of Array the way you are, it's very messy. I suggest using a list of arrays like this:
var arrays:Vector.<Array> = new <Array>[];
for(var i = 0; i < 8; i++)
{
arrays.push(new Array(50));
}
Where you would access an array like this:
var inner:Array = arrays[2];
And values of the arrays using [x][y]:
trace(arrays[0][0]);
Related
I've a array of list and created image place holder with the array objects name (Example: myArray[a,b]) and loaded images from there, having array list as folder name.
And finally created a movieClip to load on them and display in stage.
Now I want to remove them calling the same array list.
Need help here...
My code below:
for (var l: int = 0; l < ListArray.length; l++) {
var BookName: MovieClip = new bookThumb();
trace("\n[" + l + "]: " + ListArray[l]);
ImageFoldername = ListArray[l];
var bookImagePath: String = "file://" + File.userDirectory.nativePath.toString() + "/Books/" + ImageFoldername + "/Images/Icon.png";
trace(bookImagePath);
var ImagePlacer: Loader = new Loader;
var ImageURL: URLRequest = new URLRequest(bookImagePath);
ImagePlacer.load(ImageURL);
BookName.addChild(ImagePlacer);
BookName.name = ImageFoldername
addChild(BookName)
BookName.x = FirstBook_x;
BookName.y = FirstBook_y;
BookName.buttonMode = true;
BookName.mouseChildren = false;
BookName.addEventListener(MouseEvent.CLICK, IconSelected);
BookName.alpha = .8
BookName.addEventListener(MouseEvent.MOUSE_OVER, IconMouseOver);
BookName.addEventListener(MouseEvent.MOUSE_OUT, IconMouseOut);
FirstBook_x = FirstBook_x + 250;
}
Because you assign the ImageFoldername to each MovieClip's name property, you can use getChildByName to find them and remove them:
(Note: convention in AS3 is to use UpperCamelCase for classes, and lowerCamelCase for variables and functions. I've written the code below following this convention. )
for (var i:int = 0; i < listArray.length; i++) {
var imageFolderName:String = listArray[i];
var bookThumb:BookThumb = getChildByName(imageFolderName) as BookThumb;
if (bookThumb) {
removeChild(bookThumb);
}
}
Another way you can handle it is to add each book thumb into a list:
var bookThumbs:Array = [];
for (var i:int = 0; i < listArray.length; i++) {
var bookThumb:Bookthumb = new BookThumb();
bookThumbs.push(bookThumb);
// ...
}
Then to remove them all:
for each (var bookThumb:BookThumb in bookThumbs) {
removeChild(bookThumb);
}
bookThumbs.length = 0;
But probably the easiest way to remove them all would be to simply put them all in a single container and later use removeChildren() to remove them all:
var container:Sprite = new Sprite();
addChild(container);
for (var i:int = 0; i < listArray.length; i++) {
var bookThumb:Bookthumb = new BookThumb();
container.push(bookThumb);
// ...
}
// when you want to remove them all:
container.removeChildren();
I would like to add "RealData" to my array "tempArr", I am trying to use "push" but I have no experience with it so I can not get it to work. Could you help me out?
for (var i=0; i < data.length; i++) {
var testDate = subDaysFromDate(data[i][0],1)
var DateToCheck = Utilities.formatDate(testDate, "GMT", "dd-MM-yyyy");
var DateToCheckBefore = DateToCheck.split("-");
var DateToCheckAfter = new Date(DateToCheckBefore[2], DateToCheckBefore[1]-1, DateToCheckBefore[0]); //dit is de datum van een row
Row++;
var tempArr = new Array();
var d1 = $d1.split("-");
var d2 = $d2.split("-");
var from = new Date(d1[0], d1[1]-1, d1[2]); // -1 because months are from 0 to 11
var to = new Date(d2[0], d2[1]-1, d2[2]);
if(DateToCheckAfter >= from && DateToCheckAfter <= to){
var KlantNr = sheet.getRange("A"+Row).getValue();
var KlantVoornaam = sheet.getRange("B"+Row).getValue();
var KlantAchternaam = sheet.getRange("C"+Row).getValue();
var HTMLData = KlantNr + "-" + KlantVoornaam + "-" + KlantAchternaam;
var RealData = HTMLData.split("-");
tempArr.push(RealData);
}
}
Logger.log(tempArr);
When you PUSH an array into another you're referencing it, to copy use slice:
tempArr.push(RealData.slice(0));
Also, this is a javascript problem.
I'm trying to add multiple copies of the same movieclip to the stage at once
I have the loop which fills the array and generate the movieclips to the stage
and the loop to add The click EventListener for each movieClip
but I miss the magical code to access every MovieClip separately
to have it removed from the stage by clicking on it
var numOfClips:Number = 5;
var mcArray:Array = new Array();
for(var i=0; i<numOfClips; i++)
{
var usd:mcUSD = new mcUSD();
//genrate random x , y position----------------------------
var randY:Number = Math.floor(Math.random()*460) + 120;
var randX:Number = Math.floor(Math.random()*350) + 60;
usd.x = randX;
usd.y = randY;
//---------------------------------------------------------
mcArray.push(usd);
addChild(usd);
}
for(var m:int = 0; m<mcArray.length; m++){
usd.addEventListener(MouseEvent.CLICK, colectmoney);
}
function colectmoney(e:MouseEvent): void {
removeChild(usd);
}
Try this:
import flash.events.MouseEvent;
var numOfClips:Number = 5;
var mcArray:Array = new Array();
for(var i=0; i<numOfClips; i++)
{
var usd:mcUSD = new mcUSD();
//genrate random x , y position----------------------------
var randY:Number = Math.floor(Math.random()*460) + 120;
var randX:Number = Math.floor(Math.random()*350) + 60;
usd.x = randX;
usd.y = randY;
//---------------------------------------------------------
mcArray.push(usd);
addChild(usd);
}
addEventListener(MouseEvent.CLICK, mouseClickHandler);
function mouseClickHandler(e:MouseEvent) : void {
removeChild(mcArray[mcArray.indexOf(e.target)]);
}
Important things to note: 1) you do not need to call a mouse.click event listener for each mcUSD object. It's more efficient to call it once. 2) removeChild(usd) won't work because you need to tell AS3 which mcUSD object to remove. 3) try to keep function nomenclature consistent - eg colectMoney instead of colectmoney. it will save you confusing times once your program gets bigger. hope this helps! :)
I have a string : a - b - c and I would like to create a array.
A [] >> B [] >> C []
var f:Array = "a-b-d".split('-');
var tree:Array = new Array;
for (var i:Number=0; i < f.length ; i++)
{
var o:Object = new Object
o.name = f[i];
o.path = f.slice(0,i);
o.isDirectory = (i == f.length)? false :true ;
}
I would like this
var x:Array = new Array(new Array(new Array()));
Is this the right way? Or how could I do?
Yes, you can create a multi-dimensional array within arrays. It might be useful to create an "Object" instead, and have Array as collections for those objects, given your data model. And it may be useful to create helper methods to search, retrieve, update, and delete items within that structure.
The problem is how is your string structured? Is it only 3 levels deep? This is untested code, and based on assumptions you've laid out that your string structure is only a set level deep.
Here's how, using your code:
var f:Array = "a-b-c".split('-');
// create first dimension of arrays (tree)
var tree:Array = new Array();
var secondTree:Array = new Array();
var thirdTree:Array = new Array();
secondTree.push(thirdTree);
tree.push(secondTree);
// create second dimension of arrays (tree)
for (var i:int=0; i < f.length; i++)
{
var o:Object = new Object
o.name = f[i];
o.path = f.slice(0,i);
o.isDirectory = (i == f.length)? false : true;
if(i%3 == 0) {
thirdTree.push(o);
} else if(i%2 == 0) {
secondTree.push(o);
} else {
tree.push(o);
}
}
How can I splice an array within another array?
I'm trying to create a game for kids in my class. Some kind of a trivia history question creator. How to splice the MasterArray so that I would get rid of sub-arrays (Hard-England and Medium-England) within the allEnglandArray. Because the "MasterArray.splice()" seem to be affecting - splicing only the allEngland array, or the allFrance array. But I need to get rid of those sub-arrays...
My code:
var Easy-England:Array = ["item1","item2","item3","item4","item5"];
var Medium-England:Array = ["item6","item7","item8"];
var Hard-England:Array = ["item9","item10"];
var allEngland:Array = [Easy-England,Medium-England,Hard-England];
var Easy-France:Array = ["item11","item12","item13","item14","item15"];
var Medium-France:Array = ["item16","item17","item18"];
var Hard-France:Array = ["item19","item20"];
var allFrance:Array = [Easy-France,Medium-France,Hard-France];
// the list of countries goes on and on and on... (Italy, Hungary, etc.)
var allStuff:Array = [allEngland, allFrance, etc.];
var MasterArray:Array;
// FUNCTIONS
// clear MasterArray - first I clear out completely the MasterArray
function clearMasterArray():void
{
MasterArray.splice(0);
}
// update MasterArray - than I fill the MasterArray with data according to checkBoxes
function updateMasterArray():void
{
for (var i:int = 0; i<checkBoxBlock.myCheckBoxes.length; i++)
{
if (checkBoxBlock.myCheckBoxes[i].selected)
{
MasterArray.push(allStuff[i]);
}
}
}
// splice MasterArray - last thing I do is splice the items according to student's proficiency level, referred to as "studentPL".
function spliceMasterArray():void
{
if (studentPL == 1)
{
for (var i:int = 0; i<allStuff.length; i++)
{
allStuff[i].splice(5,5);
}
}
if (studentPL == 2)
{
for (var i:int = 0; i<allStuff.length; i++)
{
allStuff[i].splice(8,2);
}
}
if (studentPL == 3)
{
for (var i:int = 0; i<allStuff.length; i++)
{
trace("no need to splice");
}
}
}
And after this I call those functions in another function in this order...
function creatorFunction():void
{
clearMasterArray();
updateMasterArray();
spliceMasterArray();
}
Instead of:
var allEngland:Array = [Easy-England,Medium-England,Hard-England];
try this:
var allEngland:Array = Easy-England.concat(Medium-England, Hard-England);
This way you will have a 'flat' array (no sub-arrays), so it will be easier to deal with.