A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: array shuffle...

  1. #1
    Junior Member
    Join Date
    Feb 2006
    Posts
    23

    array shuffle...

    Well im working on this teaser campaign for an event thats happening, and i dont know how to go about setting it up so that when someone comes onto the site, a different clip plays...we have 4 clips, and everytime the person refreshes a different clip comes up...how do i set this up?

  2. #2
    Ninja Rider Jhonte's Avatar
    Join Date
    Sep 2004
    Location
    Uppsala,Sweden
    Posts
    253
    use the random and length functions.

    a example
    Code:
    var my_array:Array = new Array();
    my_array.push("one");
    my_array.push("two");
    my_array.push("tree");
    
    _root.onEnterFrame = function(){
    var rand =my_array[Math.round(Math.random()*(my_array.length-1))];
    trace(rand);
    }
    that randomises at the length of the array.
    Show me the way to the next whiskeybar ...

  3. #3
    Junior Member
    Join Date
    Feb 2006
    Posts
    23
    Thanx for the speedy reply but i dont see any code?

  4. #4
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    The method Jhonte posted will work but it has the possibility of repeating the same random value. In order to prevent repeating values you can splice the currently selected value from the array. This assures it can't be repeated back to back while the movie is playing. However, this wont work for refreshing the page as the array will be refreshed each time, so there will still be a possibility of repeat.

  5. #5
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    You can use a Local Shared Object to keep track of the last clip the user watched, and play the next.
    Considering you have the clips in the array my_array:
    code:

    // create the shared object
    var my_so = SharedObject.getLocal("LastPlayedSO");

    // see if the shared object already exists
    if (my_so.data.lastPlayed == undefined) {
    // doesn't exist
    my_so.data.lastPlayed = 1; // initialize
    trace("doesn't exist");
    } else if (my_so.data.lastPlayed == 4)
    {
    // the last played was the last, reset
    my_so.data.lastPlayed = 1;
    }else {
    my_so.data.lastPlayed++;
    // exists
    // if you test the movie again, this will be triggeres
    }
    trace('lastPlayed -> ' + my_so.data.lastPlayed);

    // play the clip: my_array[my_so.data.lastPlayed-1]
    // add your code here


  6. #6
    Junior Member
    Join Date
    Feb 2006
    Posts
    23
    thank you so much, but another problem is that i haven't set up my_array, how would i incorporate this into my site.

  7. #7
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    That depends on what you want to do. It was you who brought the array in.


    My idea is that you can have an array with the names of the movies you want to play.
    I corrected and changed the code I posted before. No matter how many elements your array has, you don't have to change anything else.

    code:

    // array with the movies to play
    var my_array = new Array("first.swf", "second.swf", "third.swf");
    var numberOfMovies = my_array.length;

    // create the shared object
    var my_so = SharedObject.getLocal("LastPlayedSO");

    // see if the shared object already exists
    if (my_so.data.lastPlayed == undefined) {
    // doesn't exist
    my_so.data.lastPlayed = 1; // initialize
    trace("doesn't exist");
    } else if (my_so.data.lastPlayed == numberOfMovies)
    {
    trace("reset");

    // the last played was the last, reset
    my_so.data.lastPlayed = 1;
    }else {
    my_so.data.lastPlayed++;
    // exists
    // if you test the movie again, this will be triggeres
    }
    trace('lastPlayed -> ' + my_so.data.lastPlayed);

    // play the clip: my_array[my_so.data.lastPlayed-1]
    // which may correspond to loading the specific movie
    loadMovieNum(my_array[my_so.data.lastPlayed-1], 1);


  8. #8
    Junior Member
    Join Date
    Feb 2006
    Posts
    23

    Perfect

    works great thanx so much

  9. #9
    Junior Member
    Join Date
    Feb 2006
    Posts
    23
    this works great so i have exported the videos and made 3 separate flash videos and the main movie witha preloader, only one problem sometimes it goes through all movies in a row and it gets really quirky, is that just because of the preloader? its getting really wacked out.

  10. #10
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    If that happens, it looks like that the preloader is making the movies load repeatdly, which may be because the main movie (the movie which has the code attached to) is being played repeatly, i.e., the code is being triggered more than once.

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