A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: array help

  1. #1
    Member
    Join Date
    Apr 2006
    Posts
    30

    array help

    Hi,

    I have an image gallery that has arrows that go forward and back through the images.

    The images are loaded in via xml, with headers and descriptions also loading in via xml.

    This all works perfectly.

    Now I want to randomise (with no repeats) the image with the corresponding header etc.

    I have got it working with a random variable, if i randomise each variable it matches the copy with the image and by using splice stops the repeating, the only problem is that this removes them and then i cant go back through the images.

    Is there a better way? I have tried shuffle but this doesn't match the copy as each array shuffles different.

    I am sure it must be possible.

    any help greatly appreciated.

    Thanks

  2. #2
    Getting There! bitsk308's Avatar
    Join Date
    Jul 2000
    Location
    Phoenix, AZ
    Posts
    427
    Could you just push the number back into the array immediately after you've spliced it out?

  3. #3
    Member
    Join Date
    Apr 2006
    Posts
    30
    thanks for the reply.

    can you do that?

    image.splice(ranNum ,1)

    Is my splice code, do i just put push code underneath?

  4. #4
    Getting There! bitsk308's Avatar
    Join Date
    Jul 2000
    Location
    Phoenix, AZ
    Posts
    427
    Wait i just realized i don't completely understand what you're after. You want a random selection with no repeats, but the problem is that number comes out so you can't repeat it?

  5. #5
    Member
    Join Date
    Apr 2006
    Posts
    30
    kind of,

    Say i have 8 images, you flick through the 8 images that select in a random order.

    I want to be able to flick back through the images. I have been using splice to stop repeats when flicking forward through the images but I have found this removes them so there are no images to flick back through.

    Hope that makes a bit more sense.

    If i use push instead of splice does that stop it repeating?

  6. #6
    Getting There! bitsk308's Avatar
    Join Date
    Jul 2000
    Location
    Phoenix, AZ
    Posts
    427
    I see. Instead of randomly going through a linear array, try linearly going through a random array. Does that make sense? Instead of having an array of 1,2,3,4,5,6,7,8 and randomly getting the 7th element, then the 4th, then the 8th. Trying using your primary array to make a random array, 7,4,8,3,2,5,1,6 for example, then just cycle through this array one at a time, back and forth.

    _b

  7. #7
    Member
    Join Date
    Apr 2006
    Posts
    30
    ok, that sounds more like it,

    do i do this with shuffle?

    I have tried this but then the image array and header array shuffle differently so they dont match.

    I there away of shuffling the array the same or something?

  8. #8
    Getting There! bitsk308's Avatar
    Join Date
    Jul 2000
    Location
    Phoenix, AZ
    Posts
    427
    it's hard to offer specifics without seeing your code, but I would imagine it like this. You have a few arrays, all with the same length, whose content matches according to element id. So descritpionArray[3] = imageArray[3], right?

    So i would grab the length of one of the arrays, populate a linear array with those numbers, then make a random array from which to sample numbers from this linear one:

    var limit = imageArray.length;
    var linearArray:Array = new Array()
    var randomArray:Array = new Array()

    for(var l=0;l<limit;l++) linearArray[l] = l;
    for(var r=0;r<limit;r++) randomArray[r] = linearArray.splice(Math.floor(Math.random()*linear Array.length),1);

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