A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: I have decided...

  1. #1
    file not found Captain_404's Avatar
    Join Date
    Apr 2006
    Posts
    457

    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?

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    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?

  3. #3
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    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);
    }

    http://www.birchlabs.co.uk/
    You know you want to.

  4. #4
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    Quote 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 >.<

  5. #5
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    @ 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

  6. #6
    file not found Captain_404's Avatar
    Join Date
    Apr 2006
    Posts
    457
    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.

  7. #7
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,361
    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)

  8. #8
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    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

  9. #9
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,361
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center