A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: "Random" Sequence of Movie Clips

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    2

    "Random" Sequence of Movie Clips

    I'm pretty much on a beginner ("low intermediate" at best) level with AS, but not really new to Flash. But I need to do something outside of my scope. I am on CS3 using AS2, and I currently have a series of 19 movie clips, each placed on a keyframe of the main timeline. I need them to play randomly, but not really ... the desire is to have 1-19 play in a random order each time the main swf loads, but without repeating any of the 19. Then, after it has gone through 19, it would start the process all over again, 1-19 but in a random sequence (after the first 19, it doesn't really matter if it plays in a different sequence or not, so long as it plays initially in a different sequence each time). Here is the code that I have in the first keyframe:

    stop();

    var framesArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]; //if you add more frames .. just add it to the array.
    var myNum:Number = random(framesArr.length); //based on your array.. incase you add more frames.
    var desiredFrame = framesArr[myNum]; //stores the desired frame number to jump to.

    function onEnterFrame(){
    if(_framesloaded = desiredFrame+1){ // so it makes sure the odd frame is loaded too..
    delete onEnterFrame;
    gotoAndPlay(framesArr[myNum]);
    }
    }


    Then, on the final frame of each of the movie clips placed in the subsequent keyframes, I am simply sending the main timeline back to the first keyframe (gotoAndPlay 1). So it's essentially going to the first keyframe and playing a random keyframe over and over again. The problem is that since it is truly random, sometimes the same movie clips are playing over and over again, sometimes consecutively. The client is not too fond of that! Any assistance would be greatly appreciated!

    Mark

  2. #2
    Musical chair sumwungye's Avatar
    Join Date
    Jan 2008
    Location
    Planet Earth, third planet from the Sun
    Posts
    287
    I'll be back to see if anybody can figure this out, for I have a similar (well, not similar, more like exactly alike) problem...
    --SumWunGye

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    2
    Sounds like your problem is "exactly similar"!?!? (That's one of my pet phrases.)

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    This works in MX2004, don't know if it will work in the newer versions...
    Code:
    stop();
    if (desiredFrame == undefined) {
    	//if you add more frames .. just add it to the array.
    	var framesArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
    	framesArr.sort(function (a, b) {
    		return Math.round(Math.random() * 2) - 1;
    	}, Math.round(Math.random()) * 2);
    	trace(framesArr);
    }
    // gets the first randomized value
    desiredFrame = framesArr.shift();
    // recycles the value to the end
    framesArr.push(desiredFrame);
    //
    function onEnterFrame() {
    	if (_framesloaded = desiredFrame + 1) {
    		// so it makes sure the odd frame is loaded too..
    		delete onEnterFrame;
    		gotoAndPlay(desiredFrame);
    	}
    }

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