A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: help with Array - knowing if slot is already taken

  1. #1
    Senior Member
    Join Date
    Feb 2004
    Posts
    113

    help with Array - knowing if slot is already taken

    Hello, I will try to explain what i am would like to achieve here.. maybe someone has a code snippet, or even the theory on how to achieve this:


    I have got 20 thumbnails on a movieclip.
    When the user clicks on a thumbnail, i would like to 'save' it into another movieclip, which can contain a limit of 6 thumbnails. Sort of like a vault, or shopping cart style.
    But the vault is strictly a maximum of 6 thumbnails.

    Also, underneath each thumbnail, i'd like to have a 'remove' button, so that the selected thumb can be deleted from the 'vault'.

    My problem is, i would like to work with arrays. So i'd have arrays 1-6, and when a thumb is sent to the vault, a new array is created and the thumb name will be in the first part of an array

    e.g.
    new Array("123456.jpg", 0, 0, 0, 0, 0, 0);

    Does anyone know how i can achieve this?

    to explain further i can give an example.

    The vault is full of all 6 thumbnails, and the user deletes thumbnail 3.

    so now when the user clicks on a new thumb, how do i tell Flash, that there is an array/thumbnail slot avaible to be filled with values?


    *****Edit******
    http://www.yoce.com/thevault.zip
    better explain the layout etc, i've created a small flash file with images loaded into it and the vault seciton on the right

    what i need to add to it is when you click the thumbs, it will put that image in the slots in the vault, but display and error if it is full already
    and then the ability to delete and free up one of the slots, so one can be added again...

    someonehow i need to create an array for each image that is put into the vault.. then i can add more details for it later..


    here is the flash i made it's not perfect but small and simple!

    http://www.yoce.com/thevault.zip

  2. #2
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    it should be fairly easy I believe..

    you can ALWAYS check an arrays length.

    totalSlots = arrayName.length;
    trace("total available: "+totalSlots);

    you can do a simple check on the array.length; to see if it is at 6 (full) or not.. if not full add (push) the next piece of data into the array slot (index).

    note: you keeping saying array when I think you mean to be describing adding index/attribute to the array. an ARRAY holds multiple pieces of information.... so your not really 'adding an array'..your adding TO THE array. (make sense?)

  3. #3
    Senior Member
    Join Date
    Feb 2004
    Posts
    113
    to be honest, i can't even get the thumbnail to add to the vault when you click on it, the array is just something i want to create at the same time the thumbnail is added to the 'vault'

    it's difficult!

  4. #4
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    A simple idea might be to set the values of the array to "null" when there is nothing inside:

    code:

    var array:Array = [null, null, null, null, null, null];

    trace(array.length);



    Notice the array still has a length on "6". Now when you want to add a new thumbnail to the array, all we need do is find the first empty value:

    code:

    function nextEmpty(a:Array):Number {
    for (var i = 0; i < a.length; i++) {
    if (a[i] == null) {
    return i;
    }
    }
    return null;
    }

    trace(nextEmpty(array));




    We see in this case that the next empty element is the 0'th element. To add to the array, we simply use this value:

    code:


    var n = nextEmpty(array);

    //If there is an empty element
    if (n != null) {
    //put something in it
    array[n] = "something";
    }

    trace(nextEmpty(array));




    A more sophisticated method would be to extend the properties of the Array class to include a new method that handles this behaviour:

    code:

    var array:Array = [null, null, null, null, null, null];

    Array.prototype.addToEmpty = function(o:Object):Void {
    for (var i:Number = 0; i < this.length; i++) {
    if (this[i] == null) {
    this[i] = o;
    break;
    }
    }
    if (i == 6) {
    trace("Array is full");
    }
    }

    array.addToEmpty(1);

    trace(array);



    An even more sophisticated method would be to write a custom class, but this involves more work again. As a sidenote, you should consider using a custom object for the thumbnail instead of an array:

    code:

    function createThumbnail(p:String):Object {
    var t:Object = new Object();
    t.path = p;
    return t;
    }

    //To create an object, we can write this:
    thumb = createThumbnail("123456.jpg");

    array.addToEmpty(thumb);
    trace(array);

    trace(array[0].path);



    You can add as many properties as you like, and it makes a bit easier to find things in the object than in an array.

    Hope this helps.
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

  5. #5
    Senior Member
    Join Date
    Feb 2004
    Posts
    113
    thanks Dmonkey
    i'll take this code and see what i can do!

  6. #6
    Senior Member
    Join Date
    Feb 2004
    Posts
    113
    dmonkey, is it possible to do it similar to what you're talking about, but when an image is added to the vault, it doesnt add its name to an array, but it creates a new for itself?
    Because i hope to have arrays formed like

    array1 (imagecode, option1, option2, option3, amount, size)

    because this is just the first step. Once the image is added to the vault and they press the 'continue with selections' button, they will be taken to a new page with only their 1,2,3,4,5 or 6 images and once they click on the image, they have to choose 5 different options.

    so i'd like to save those 5 options they choose into the array for each thumbnail

    so i can then add it to the shopping cart with all relevent info


    i hope you understand me!

  7. #7
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    Yes. Create your array:

    code:

    var array1:Array = [imagecode, option1, option2, option3, amount, size];



    Then add it to the other array like this:

    code:

    array.addToEmpty(array1);



    or using the simpler method:

    code:

    .
    .
    .
    array[n] = array1;
    .
    .
    .



    Now your thumbnail array is essentially multidimensional.

    Hope this helps.
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

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