A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: Random movie without repeat as3 Please Help!

  1. #1
    Junior Member
    Join Date
    Nov 2004
    Location
    the LBC USA
    Posts
    9

    Random movie without repeat as3 Please Help!

    A friend of mine asked for my help with this and unfortunately, I've been away from AS for too long.

    Each movie needs to load on top of the last movie played in random order without repeating the same movie. Once all nine are played, it shall start over again (not necessary to sort random again, just the first initial load)

    Here is the code that was in his file (all cleaned up) There has to be an easier way than this.

    I know it's asking a lot of someone to help but I'm not so good at going through someone code.

    On frame one



    HTML Code:
    var movieArray:Array = new Array();
    
    movieArray = ["PersonMc1","PersonMc2","PersonMc3","PersonMc4","PersonMc5","PersonMc6","PersonMc7","PersonMc8","PersonMc9"];
    
    
    
    var MovieClipContainer:Sprite=new Sprite();
    var IntroMc1:IntroMc=new IntroMc();
    var PersonMc1:Person1Mc=new Person1Mc();
    var PersonMc2:Person2Mc=new Person2Mc();
    var PersonMc3:Person3Mc=new Person3Mc();
    var PersonMc4:Person4Mc=new Person4Mc();
    var PersonMc5:Person5Mc=new Person5Mc();
    var PersonMc6:Person6Mc=new Person6Mc();
    var PersonMc7:Person7Mc=new Person7Mc();
    var PersonMc8:Person8Mc=new Person8Mc();
    var PersonMc9:Person9Mc=new Person9Mc();
    
    addChild (MovieClipContainer);
    
    MovieClipContainer.addChild (IntroMc1);
    MovieClipContainer.addChild (PersonMc1);
    MovieClipContainer.addChild (PersonMc2);
    MovieClipContainer.addChild (PersonMc3);
    MovieClipContainer.addChild (PersonMc4);
    MovieClipContainer.addChild (PersonMc5);
    MovieClipContainer.addChild (PersonMc6);
    MovieClipContainer.addChild (PersonMc7);
    MovieClipContainer.addChild (PersonMc8);
    MovieClipContainer.addChild (PersonMc9);
    
    PersonMc1.visible = false;
    PersonMc2.visible = false;
    PersonMc3.visible = false;
    PersonMc4.visible = false;
    PersonMc5.visible = false;
    PersonMc6.visible = false;
    PersonMc7.visible = false;
    PersonMc8.visible = false;
    PersonMc9.visible = false;
    
    PersonMc1.stop ();
    PersonMc2.stop ();
    PersonMc3.stop ();
    PersonMc4.stop ();
    PersonMc5.stop ();
    PersonMc6.stop ();
    PersonMc7.stop ();
    PersonMc8.stop ();
    PersonMc9.stop ();

    On Frame Two



    HTML Code:
    stop ();
    
    /*trace ("Frame 2");*/
    var prevIndex:int;
    clickStart ();
    
    function clickStart () {
    
    
    	if ((MovieClip(root).movieArray[Math.floor(Math.random() * 9)]) == "PersonMc1") {
    
    		if (MovieClip(root).movieArray == PersonMc1) {
    
    			clickStart ();
    
    		} else {
    
    			MovieClip(root).PersonMc1.visible = true;
    			MovieClip(root).PersonMc1.gotoAndPlay (1);
    			prevIndex = MovieClip(root).MovieClipContainer.getChildIndex(PersonMc1);
    
    			MovieClip(root).MovieClipContainer.setChildIndex (PersonMc1,MovieClip(root).MovieClipContainer.numChildren-1);
    			trace (prevIndex);
    			trace (MovieClip(root).MovieClipContainer.numChildren-1);
    		}
    
    
    	} else if ((MovieClip(root).movieArray[Math.floor(Math.random() * 9)]) == "PersonMc2") {
    		if (MovieClip(root).movieArray == PersonMc2) {
    			clickStart ();
    
    		} else {
    			MovieClip(root).PersonMc2.visible = true;
    			MovieClip(root).PersonMc2.gotoAndPlay (1);
    			prevIndex = MovieClip(root).MovieClipContainer.getChildIndex(PersonMc2);
    
    			MovieClip(root).MovieClipContainer.setChildIndex (PersonMc2,MovieClip(root).MovieClipContainer.numChildren-1);
    			trace (prevIndex);
    
    		}
    
    	} else if ((MovieClip(root).movieArray[Math.floor(Math.random() * 9)]) == "PersonMc3") {
    		if (MovieClip(root).movieArray == PersonMc3) {
    			clickStart ();
    
    		} else {
    			MovieClip(root).PersonMc3.visible = true;
    			MovieClip(root).PersonMc3.gotoAndPlay (1);
    			prevIndex = MovieClip(root).MovieClipContainer.getChildIndex(PersonMc3);
    
    			MovieClip(root).MovieClipContainer.setChildIndex (PersonMc3,MovieClip(root).MovieClipContainer.numChildren-1);
    			trace (prevIndex);
    
    		}
    	} else if ((MovieClip(root).movieArray[Math.floor(Math.random() * 9)]) == "PersonMc4") {
    		if (MovieClip(root).movieArray == PersonMc4) {
    			clickStart ();
    
    		} else {
    			MovieClip(root).PersonMc4.visible = true;
    			MovieClip(root).PersonMc4.gotoAndPlay (1);
    			prevIndex = MovieClip(root).MovieClipContainer.getChildIndex(PersonMc4);
    
    			MovieClip(root).MovieClipContainer.setChildIndex (PersonMc4,MovieClip(root).MovieClipContainer.numChildren-1);
    			trace (prevIndex);
    
    		}
    	} else if ((MovieClip(root).movieArray[Math.floor(Math.random() * 9)]) == "PersonMc5") {
    		if (MovieClip(root).movieArray == PersonMc5) {
    			clickStart ();
    
    		} else {
    			MovieClip(root).PersonMc5.visible = true;
    			MovieClip(root).PersonMc5.gotoAndPlay (1);
    			prevIndex = MovieClip(root).MovieClipContainer.getChildIndex(PersonMc5);
    
    			MovieClip(root).MovieClipContainer.setChildIndex (PersonMc5,MovieClip(root).MovieClipContainer.numChildren-1);
    			trace (prevIndex);
    
    		}
    	} else if ((MovieClip(root).movieArray[Math.floor(Math.random() * 9)]) == "PersonMc6") {
    		if (MovieClip(root).movieArray == PersonMc6) {
    			clickStart ();
    
    		} else {
    			MovieClip(root).PersonMc6.visible = true;
    			MovieClip(root).PersonMc6.gotoAndPlay (1);
    			prevIndex = MovieClip(root).MovieClipContainer.getChildIndex(PersonMc6);
    
    			MovieClip(root).MovieClipContainer.setChildIndex (PersonMc6,MovieClip(root).MovieClipContainer.numChildren-1);
    			trace (prevIndex);
    
    		}
    	} else if ((MovieClip(root).movieArray[Math.floor(Math.random() * 9)]) == "PersonMc7") {
    		if (MovieClip(root).movieArray == PersonMc7) {
    			clickStart ();
    
    		} else {
    			MovieClip(root).PersonMc7.visible = true;
    			MovieClip(root).PersonMc7.gotoAndPlay (1);
    			prevIndex = MovieClip(root).MovieClipContainer.getChildIndex(PersonMc7);
    
    			MovieClip(root).MovieClipContainer.setChildIndex (PersonMc7,MovieClip(root).MovieClipContainer.numChildren-1);
    			trace (prevIndex);
    
    		}
    	} else if ((MovieClip(root).movieArray[Math.floor(Math.random() * 9)]) == "PersonMc8") {
    		if (MovieClip(root).movieArray == PersonMc8) {
    			clickStart ();
    
    		} else {
    			MovieClip(root).PersonMc8.visible = true;
    			MovieClip(root).PersonMc8.gotoAndPlay (1);
    			prevIndex = MovieClip(root).MovieClipContainer.getChildIndex(PersonMc8);
    
    			MovieClip(root).MovieClipContainer.setChildIndex (PersonMc8,MovieClip(root).MovieClipContainer.numChildren-1);
    			trace (prevIndex);
    
    		}
    
    	} else if ((MovieClip(root).movieArray[Math.floor(Math.random() * 9)]) == "PersonMc9") {
    		clickStart ();
    
    	} else {
    		MovieClip(root).PersonMc9.visible = true;
    		MovieClip(root).PersonMc9.gotoAndPlay (1);
    		prevIndex = MovieClip(root).MovieClipContainer.getChildIndex(PersonMc9);
    
    		MovieClip(root).MovieClipContainer.setChildIndex (PersonMc9,MovieClip(root).MovieClipContainer.numChildren-1);
    		trace (prevIndex);
    
    	}
    
    
    
    
    }
    Thanks in advance for your time! I really appreciate the help.

    Thom
    www.tdcollins.com

  2. #2
    Junior Member
    Join Date
    Nov 2004
    Location
    the LBC USA
    Posts
    9
    Isn't there a way to generate a random list from that array, play the movie, load the next one above that last played, and just have one generic function to handle it? The way the movies work is that masks animate and cover the previously played movie. There has to be a simpler way...
    www.tdcollins.com

  3. #3
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Yeah, you need to use loops. It's programming so I don't know if you want to get into learning it or not.

    Here's code I made. I can't spend any more time. This code is one way to get a random index. I've tried to keep the code as close to yours.

    Code:
    movieArray = ["PersonMc1", "PersonMc2", "PersonMc3", "PersonMc4", "PersonMc5", "PersonMc6", "PersonMc7", "PersonMc8", "PersonMc9"];
    
    //  new array to keep track of what's been played
    aMoviePlayed: Array = new Array(false, false, false, false, false, false, false, false, false);
    
    //  var names use first letter to describe what type of var
    function fGetRandom(): void {
    	var i: int;
    	var bNeedUnplayedMovie: Boolean = true;
    	var iRandomIndex: int = (Math.random() * movieArray.length);
    
    	while (bNeedUnplayedMovie) {
    		iRandomIndex = (Math.random() * movieArray.length);
    		if (aMoviePlayed[iRandomIndex] == false) {
    			//  this movie hasn't played yet, play iRandomIndex movie			
    			aMoviePlayed[iRandomIndex] = true;  // mark it as played
    			MovieClip(root)["PersonMc" + iRandomIndex].visible = true;
    			MovieClip(root)["PersonMc" + iRandomIndex].gotoAndPlay(1);
    			prevIndex = MovieClip(root).MovieClipContainer.getChildIndex("PersonMc" + iRandomIndex);
    
    			MovieClip(root).MovieClipContainer.setChildIndex("PersonMc" + iRandomIndex, MovieClip(root).MovieClipContainer.numChildren - 1);
    			trace(prevIndex);
    			trace(MovieClip(root).MovieClipContainer.numChildren - 1);
    
    		}
    		//  if the random index has been played, nothing happens.
    		//  loop back and get a new index
    	}
    }
    Last edited by moot; 05-30-2014 at 10:41 AM.

  4. #4
    Junior Member
    Join Date
    Nov 2004
    Location
    the LBC USA
    Posts
    9
    Thanks moot!

    How does this fit it with the existing code? On frame 1 or frame 2?

    Sorry, this is over my head a bit... appreciate your patience.
    www.tdcollins.com

  5. #5
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    The function should randomly select an index, if that index hasn't been played, play it. It uses your code. In general, you can call this instead of your clickStart().

    I don't know when I'll have more time. Can somebody help?

    You can get quicker response at adobe.com forum.

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Perhaps I can help along too, if you could attach your *.fla we can see what is going on as it is awkward trying to emulate somebodies file.

    If it's too big maybe create a skeleton version of what needs to be achieved.

  7. #7
    Junior Member
    Join Date
    Nov 2004
    Location
    the LBC USA
    Posts
    9
    http://tdcollins.com/slideShow.zip

    Here is the skeleton version. And thanks Fruitbeard?

    I had to take out all the images otherwise it would be too large I think. But as you can see... it works fine enough unless there is a duplicate of the same clip that plays. I would hope this would be simple, but for me it's not. It would be great if it would just randomize the list once and just play over and over again... but the initial is always a new random list. Is that possible?
    www.tdcollins.com

  8. #8
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Which version of Flash do you have as I can only save as low as CS5,

    The version will continually be random, i hope you wil be able to make sense of the code, it's not so different from Moots, although I splice(remove) the current playing movie from the array leaving only the movies in the array still to play.

  9. #9
    Junior Member
    Join Date
    Nov 2004
    Location
    the LBC USA
    Posts
    9
    I have CS5.5 at home but at work I have the latest version. I used to love working in flash but as I got older, I couldn't keep up. I felt bad because my friend hired someone to do this for them and when they got frustrated, abandoned the project. He asked me to see what I can do but honestly, I am still just learning AS3 but can animate the heck out of it, but I'm no programmer. AS2 is all I really know kinda well. But those days are gone... sad...
    www.tdcollins.com

  10. #10
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Heres the link, I will remove it after a short while, http://fruitbeard.net/slideCopy.zip

    Please feel free to mess around with it and change it too.

  11. #11
    Junior Member
    Join Date
    Nov 2004
    Location
    the LBC USA
    Posts
    9
    random does work, but the previous movie needs to be still visible while the current one plays over it. The current clip is covered up via animation over the previous played. Does that make sense?

    If you want, I can email you the real file directly so you can see how it works... it's 2 megs.. but you can see how it works, until a duplicate movie is played... then it looks like a glitch
    Last edited by tdc_longbeach; 05-31-2014 at 07:40 AM.
    www.tdcollins.com

  12. #12
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    The keeping visible part is simiple, however, how do you wish it to loop back through again?
    All invisible again and start from the intro?

  13. #13
    Junior Member
    Join Date
    Nov 2004
    Location
    the LBC USA
    Posts
    9
    loop yes, all the clips BUT the intro... but random
    Last edited by tdc_longbeach; 05-31-2014 at 07:53 AM.
    www.tdcollins.com

  14. #14
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Here we go, I hope this is what you require, http://fruitbeard.net/revised.zip

  15. #15
    Junior Member
    Join Date
    Nov 2004
    Location
    the LBC USA
    Posts
    9
    OMG YES! you. are. BRILLIANT!
    www.tdcollins.com

  16. #16
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I'm not sure if you get my message replies or not??

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