A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Randomly load external SWF's

  1. #1
    Senior Member Sir Yendor's Avatar
    Join Date
    Sep 2001
    Location
    Over here
    Posts
    1,140

    Randomly load external SWF's

    It's been quite a while since I've done anything with Flash. A lot is coming back to me, but a lot has changed.

    I need to call external SWF's so they randomly appear within my main SWF. When one ends, the next will start and so on. I searched the forum and found a thread with the code below and have run in to an issue.

    Each of the external clips is set up exactly the same... same length, same tweens, etc.

    The code below is sitting the first frame, on the top layer within my main SWF:

    randomClips = new Array ("Journey.swf", "Doobies.swf", "LedZep1.swf", "PinkFloyd1.swf", "Stones.swf");
    function randomBackground() {
    randomNumber = random (randomClips.length);
    _root.mtClip.loadMovie (randomClips[randomNumber]);
    }
    randomBackground();

    The main movie is one frame long and has three layers. The top layer has the Action Script (above), the bottom layer has my background image, and the middle layer holds the empty movie clip named mtClip

    When loaded to the server, the movie plays and one of the external SWF's will load. When it ends, I need one of the other SWF's to load. What happens is that rather than randomly playing one of the other SWF's, it repeats the original external SWF. If I refresh the main SWF, it randomly selects one of the external SWF's and it will constantly repeat.

    The external SWF's have no Action Script. I tried it with putting a Stop on the last frame, but that didn't help.

    Any ideas?

    Thanks

  2. #2
    Senior Member
    Join Date
    May 2002
    Posts
    1,017
    Try this:

    Actionscript Code:
    randomClips = new Array ("Journey.swf", "Doobies.swf", "LedZep1.swf", "PinkFloyd1.swf", "Stones.swf");

    var mclListener:Object = new Object();

    mclListener.onLoadInit = function(target_mc:MovieClip) {
       target_mc.onEnterFrame = function() {
            if (this._currentframe == this._totalframes) {
                  delete this.onEnterFrame;
                  _root.randomBackground();
            }
       }
    }

    function randomBackground() {
        randomNumber = random (randomClips.length);
            randomClipsLoader.loadClip(randomClips[randomNumber], _root.mtClip);
    }

    var randomClipsLoader :MovieClipLoader = new MovieClipLoader();
    randomClipsLoader.addListener(mclListener);

    randomBackground();
    while (2+2 == 4) {
    drink ("bourbon");
    }
    gotoAndStop("bed");

  3. #3
    Senior Member Sir Yendor's Avatar
    Join Date
    Sep 2001
    Location
    Over here
    Posts
    1,140
    Thanks, I think that will do it. I replaced my code with yours and it is working but sometimes it will play the same external clip back to back. I've only got five externals so I will add a few more and that should reduce the chance for the same one to play back to back.

    Thanks for the help!

    Update... I added several more and it works perfectly. Thanks again
    Last edited by Sir Yendor; 08-19-2010 at 11:23 AM.

  4. #4
    Senior Member
    Join Date
    May 2002
    Posts
    1,017
    You are welcome
    while (2+2 == 4) {
    drink ("bourbon");
    }
    gotoAndStop("bed");

  5. #5
    Senior Member Sir Yendor's Avatar
    Join Date
    Sep 2001
    Location
    Over here
    Posts
    1,140
    This was a tremendous help, but I want to see if I can add a modification. Right now I only have about 8 external SWF's loading. Eventually I will have 20 or 30 but for now, only thouse 8. Because there are so few, sometime the one that just played will repeat... play right after it just played. This doesn't give the end user experience we want it to.


    Is it possible to set up the code so it knows not to repeat the same SWF? Better yet, can we tell it not to play it if it's been played in the last "x" times?

    Thanks again for all the help!

  6. #6
    Senior Member
    Join Date
    May 2002
    Posts
    1,017
    Actionscript Code:
    randomClips = new Array ("Journey.swf", "Doobies.swf", "LedZep1.swf", "PinkFloyd1.swf", "Stones.swf");
    playedClips = new Array();

    var mclListener:Object = new Object();

    mclListener.onLoadInit = function(target_mc:MovieClip) {
       target_mc.onEnterFrame = function() {
            if (this._currentframe == this._totalframes) {
                  delete this.onEnterFrame;
                  _root.randomBackground();
            }
       }
    }

    function randomBackground() {
         if (playedClips.length>3) {
             randomClips.push(playedClips.shift());
         }
         randomNumber = random (randomClips.length);
         randomClipsLoader.loadClip(randomClips[randomNumber], _root.mtClip);
         playedClips.push(randomClips[randomNumber]);
         randomClips.splice(randomNumber,1);

    }

    var randomClipsLoader :MovieClipLoader = new MovieClipLoader();
    randomClipsLoader.addListener(mclListener);

    randomBackground();
    while (2+2 == 4) {
    drink ("bourbon");
    }
    gotoAndStop("bed");

  7. #7
    Senior Member Sir Yendor's Avatar
    Join Date
    Sep 2001
    Location
    Over here
    Posts
    1,140
    Thanks, I will try this when I get back to the office where my files are tomorrow.

    Is this line:

    if (playedClips.length>3) {

    the one that controls how soon one clip will repeat? In the code above it won't let it play until at least 3 others have played? Is that correct or is there another part of the code that controls that?

  8. #8
    Senior Member
    Join Date
    May 2002
    Posts
    1,017
    Quote Originally Posted by Sir Yendor View Post
    Thanks, I will try this when I get back to the office where my files are tomorrow.

    Is this line:

    if (playedClips.length>3) {

    the one that controls how soon one clip will repeat? In the code above it won't let it play until at least 3 others have played? Is that correct or is there another part of the code that controls that?
    yes, that's right. If the array of played clips has more than 3, then get the first one and move it back to the other array.
    while (2+2 == 4) {
    drink ("bourbon");
    }
    gotoAndStop("bed");

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