|
-
file not found
I have decided...
...that I have no idea what I'm doing anymore.
I recently took a short little jaunt into the world of thinking I understood arrays, but I obviously don't. The world of thinking I understood onEnterFrame was so much more fun.
My problem is, I have two arrays, one I can make reference to in my code, the other I can't.
I defined them the exact same way, I've even removed all references to one and replaced it with the other, but I get the same result. I copied the working one and pasted it in place of the non-working and I still get trouble.
They're both multi-dimensional so you know, when I trace a line in each I get
working:
0,0,0,0,0,0,5,1,1,2,0,0,0,0,0,0,0,0
not working:
0,0,,,,,,,,,,,,,,,,
can anyone help this poor head o' mine?
-
there are some things you need to watch out with arrays
1.) always declare a new array before using it ,- esspecially in multi dimensional arrays e.g:
myArray = new Array();
for (var i=1;i<=10;i++){
myArray[i-1] = new Array();
for (var j=1;j<=10;j++){
myArray[i-1][j-1] = random(10);
}
}
2.) when you copy an array you can simply say something like:
copyArray = myArray;
because that will create a reference one (never understood that)
instead you can do it for example with this:
copyArray = new Array();
copyArray = myArray.slice(0,myArray.length);
did this helped somewhat?
-
Script kiddie
Btw, render:
PHP Code:
myArray = new Array();
for (var i=1;i<=10;i++){
myArray[i-1] = new Array();
for (var j=1;j<=10;j++){
myArray[i-1][j-1] = random(10);
}
}
I think could be simplified to this?
PHP Code:
myArray = [];
for (var i=0;i<10;i++){
myArray[i] = [];
for (var j=0;j<10;j++){
myArray[i][j] = random(10);
}
}
-
Senior Member
 Originally Posted by renderhjs
instead you can do it for example with this:
copyArray = new Array();
copyArray = myArray.slice(0,myArray.length);
...Haha. I've always made loops to copy arrays. Silly me >.<
-
@ fil_razorback:
lol me too in the beginning because I was/am confused with the reference thingy
@ VENGEANCE MX:
old habit,- but I am about to throw some of them over board now with AS3
-
file not found
Thanks everyone, I figured out what my problem was...one of those real obvious things that's hiding in plain sight n the code below it.
But thanks anyway, I think often, all you need to do to find a problem is make a thread about it. You'll find the problem yourself literally minutes afterward. Oh well.
-
Senior Member
Just a question on this topic:
I still iterate through object properties to copy them, is there any other way? (I don't believe so)
-
if you have a object can´t you simply copy it e.g:
myObj = {z:8,energy:6};
copyObj = new Object();
copyObj = myObj;
at least I dont get a mess when passing object though functions (or methods ) not like arrays who always refer to that origianl one
-
Senior Member
Try this:
code:
myObj = {z:8,energy:6};
copyObj = new Object();
copyObj = myObj;
//delete myObj;
trace(copyObj.z)
//traces 8
myObj.z=30;
trace(copyObj.z)
//traces 30
Both objects reference the same memory.
I'm trying to make a different copy. (Sorry, my original post didn't explain), not just a reference to the same thing.
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
|