|
-
DOT-INVADER
create a new array with empty slots
ok, i was just about to create an array with empty slots (but ready to be filled someway later in the game for example, like: array[5][3]="hello") using AS...
as i haven't found a simple way to do it, here's the code so far:
code:
EmptyArray = new Array()
//
function createEmptyArray(lengthX, lengthY) {
EmptyArray = [];
EmptyArray.length = lengthY;
for (i=0; i<lengthY; i++) {
EmptyArray[i] = [];
EmptyArray[i].length = lengthX;
}
}
then simply call the function like that
code: createEmptyArray(4, 2);
and it will give:
code:
EmptyArray = [[?,?,?,?],[?,?,?,?]]
// "?" here means nothing, empty
now...
what's the purpose of that thread!
1) my code is ugly and can be done in one/two line(s) of code. if so, any suggestion?
2) the code is perfect. if so, feel free to use it
-
DOT-INVADER
mmmhhh... what about being able to create a new empty array with a custom name?
code:
function createEmptyArray(name, lengthX, lengthY) {
this[name+"_array"] = new Array();
this[name+"_array"] = [];
this[name+"_array"].length = lengthY;
for (i=0; i<lengthY; i++) {
this[name+"_array"][i] = [];
this[name+"_array"][i].length = lengthX;
}
}
createEmptyArray("empty", 15, 11);
trace(empty_array.length);
-
Heli Attack!
Or:
code:
function createEmptyArray(lengthX, lengthY) {
var EmptyArray = [];
for (var i=0; i<lengthY; i++) {
EmptyArray[i] = [];
}
return EmptyArray;
}
blankArray = createEmptyArray(5,5);
That way you dont need to use any terrible slow look ups ( this[name+"_array] )..
Plus changing the .length doesnt do anything, its read only variable
Last edited by iopred; 11-21-2003 at 01:04 AM.
-
But you're not setting the lengths... lengthX is not even used in the function.
code:
Array.prototype.createEmptyArray = function(w, h) {
var arr = new Array(h);
for (var i in arr) {
arr[i] = new Array(w);
}
return arr;
}
// usage
myArray = Array.createEmptyArray(3, 5);
That way it sets the sizes that you specify, and by returning the array you can set it to anything you want, such as the name "myArray". Not tested this, just typed straight into FK, but it should work. Or you could just make it into a stand alone function instead of an Array prototype if it doesnt work.
-
Heli Attack!
extending the array object for creation is silly, because flash has to locate the array object first (prolly a 1ms difference >=)
But never the less, using new Array(num) doesnt actually do anything to set the length either, if you check your variables in flash your still getting a blank array, and the for(in) doesnt work because there is nothing in the first array =(
So really the only thing you need is:
code:
function createEmptyArray(h) {
var a = [];
for (var i = 0; i < h; i++) {
a[i] = [];
}
return a;
}
myarr = createEmptyArray(5);
the only way to force strict widths is to do another
code:
for (var j = 0; j < w; j++)
Nested inside the first, but its useless to do so really.
-
DOT-INVADER
thanks for the explanations...
the fact of waiting an additional micro-second isn't important since these arrays are created on load... but a little bit of optimisation is welcome, that's true 
anyway, iopred, your code is short and flexible... thanks :-)
-
iopred -
I wouldn't call it "silly" at all. The whole point of Math and Array, and all the other classes, is to bundle together functions of a similar nature. That function has to do with arrays, so it seemed logical to place it in the Array group of functions. This was never about the fastest possible array contructor ever.
And also, i've done some more checking, and it does seem to create array of the right size when you specify it like this: myArray = new Array(N). Check out myArray.length, and myArray.toString(). You will see that even though there is nothing in them ( and this shows in the "list variables" option, which you saw ), the array does contain N number of nothings, and has length of N.
You are right about the for-in loop though. You'd have to do a normal for-loop really.
Either way flash doesn't seem that bothered about strict array size allocation anyway, compaired to other languages...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|