A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Cannot Delete Arrays

  1. #1
    Junior Member
    Join Date
    Mar 2008
    Posts
    23

    Unhappy Cannot Delete Arrays

    I am currently making a small game using a couple of arrays, which contain MCs.

    It goes like this:

    Code:
    grid = new Array();
    for(i=0;i<array_length;i++){
    this["row"+i] = new Array();
    }
    MCs are the added for reference in the "row" arrays.

    Where I am having trouble is reseting or deleting these arrays. What I want to do is simply run those few lines again to have empty arrays using the same names. However, running those lines afterwards does nothing.

    I tried adding

    Code:
    delete grid
    grid = new Array();
    for(i=0;i<array_length;i++){
    delete this["row"+i]
    this["row"+i] = new Array();
    }
    or putting things like grid = undefined or another value... but when I use "delete this["row"+i]", the array keeps its elements.

    However, if I do the same using "delete row0", it actually gets rid of the array as intended, however the array CANNOT be re-create using "this["row"+i] = new Array();"


    The only way I see aroung this problem would be to hard code ever array as
    delete row0;
    delete row1;
    delete row2;
    ...
    and then

    row0 = new Array();
    row1 = new Array();
    ...
    But that just wrong... there's got to be things I don't understand in this situation... Any hints on what I am doing wrong ?
    Light Bringer 777

  2. #2
    Junior Member
    Join Date
    Mar 2008
    Posts
    23
    Ok messed around some more and found something I don't understand.

    I am calling both the "this[xyz]= new Array()" and "delete this[xyz]" inside and MC, and I traced something for fun...

    When I am declaring variables, tracing this._name returns "game", the MC's name... however, when deleting, tracing this._name returns undefined...

    So the problem is not about deleting arrays, which works just fine by using _root.game[xyz], but why is "this" returning undefined ?

    I used for the first time the "setTimeout" function... could it be that calling a function located inside an MC from a Timeout loses reference to the MC ?
    Light Bringer 777

  3. #3
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    to delete an array you can simply declare it again:

    e.g
    var someArray:Array = ["a" , "b", "c"];
    trace(someArray[0]) // gives "a"

    //now to 'delete' the array simply do:
    someArray = []; // or you can do someArray.length = 0;
    trace(someArray[0]) // traces undefined
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

Tags for this Thread

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