This is a discussion on Trouble getting movieclips to play at random within the Flash Actionscript Programming forums, part of the Web Development category; I desperately need help! What I want to do: I want a different movie clip to play each time the ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| I desperately need help! What I want to do: I want a different movie clip to play each time the swf file is loaded. And after the movieclip has finished playing, I would like another random movieclip to start. What I have: I have four layers, a actionscript layer. And three other layers each with one movieclip on. All these layers are on the first frame. I have placed this actionscript into the actionscript layer: However this isn't working, and is driving me mad. If anyone could help that would be great. Attach Code var clips:Array = ["Paul_mc", "Gary_mc", "Ama_mc"]; playRandomMC(); function playRandomMC() { var index:Number=Math.floor(Math.random()*clips.length ); for (var i:Number=0; i<clips.length; i++) { var mc:MovieClip=_root[clips[i]]; if (i===index) { mc._visible=true; mc.play(); } else { mc._visible=false; mc.stop(); } } } Thanks A.Ramesh |
|
#2
| |||
| |||
| Hi Ramesh, You don't say exactly what isn't working about your example. From how it looks it seems like it should, so exactly what is the problem? No, each clip doesn't need to have its own frame. Most often my files only have one frame on the main timeline and then everything else is controlled from there. Generally I would have a stop() on the first frame of each of those clips. Then I wouldn't have to worry about stopping some and playing one. If they all are stopped by default you only have to tell the one that you want to play. Below is the code I use to shuffle an array. Just put this code on a frame somewhere before you need to have your array randomized. (I usually just put it on Frame 1.) Then after you have created your clips array you can shuffle it with. clips.shuffle(); The array will now be a random order. So you can just pull out the elements one at a time to play. I'm not sure if that helps because I'm not really sure what is wrong. Post back with more details. Attach Code Array.prototype.shuffle = function() { for (var ivar = this.length-1; ivar>=0; ivar--) { var p = random(ivar+1); var t = this[ivar]; this[ivar] = this[p]; this[p] = t; } }; //Hide the shuffle prototype from the tyranny of the for…in loop ASSetPropFlags(Array.prototype,["shuffle"],1,1);
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
|
#3
| |||
| |||
| Hi, Thank you for replying. I will further explain my woes. As I have said I have four layers each on the first frame, three have a movieclip each on. The fourth is a actionscript layer. All my actionscript is on this layer, I have a stop(); at the end of each of my movieclips. As I have said I want a different movieclip to appear every time the SWF is loaded, and after one movieclip has played I would like another random movieclip to be played, etc.. I want it to be and ongoing process. The problem is that when I currently load my SWF it's just the movieclip on the top layer which is played each time. I have tried the actionscript you have suggested, thank you by the way. I placed it in the actionscript layer, however unfortunately this didn't work. Any further help you or someone else could provide would be greatly appreciated. Thank you A.Ramesh |
|
#4
| |||
| |||
| So what happens if you have only this code on your first frame? Attach Code Array.prototype.shuffle = function() { for (var ivar = this.length-1; ivar>=0; ivar--) { var p = random(ivar+1); var t = this[ivar]; this[ivar] = this[p]; this[p] = t; } }; //Hide the shuffle prototype from the tyranny of the for…in loop ASSetPropFlags(Array.prototype,["shuffle"],1,1); var clips:Array = ["Paul_mc", "Gary_mc", "Ama_mc"]; trace("The clips: "+clips); clips.shuffle(); trace("The clips: "+clips);
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
|
#5
| |||
| |||
| Hi, thanks for responding. I place the code you gave me on the first frame of the actionscript layer. The output was shown as: The clips: Paul_mc,Gary_mc,Ama_mc The clips: Paul_mc,Ama_mc,Gary_mc However when the SWF was loaded it still showed the movieclip on the top layer which is Paul_mc. Thank you for your help so far. Regards A.Ramesh |
|
#6
| |||
| |||
| Okay, so the shuffling is working. We haven't done anything about visibility or playing. Next you should make all of the clips invisible and stop them. var home:MovieClip=this; function hideClips(){ for(var i=0;i<clips.length;i++){ home[clips]._visible=false; home[clips].gotoAndStop(1); } } Finally we can do something about playing the clips. var count:Number=0; function playClip(){ home[clips[count]]._visible=true; home[clips[count]].play(); count++ if(count>=clips.length){ count=0; clips.shuffle(); } } playClips(); Of course your going to need something that checks to see when the clip is done playing. The easiest perhaps would be to put a _parent.hideClips(); _parent.playClips(); on the last keyframe of each of your clips.
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Windows XP trouble. | sanilbd | Computer Hardware | 0 | 07-17-2009 04:24 PM |
| Instances of the movieclips are disappearing | oxygen | Flash Actionscript Programming | 9 | 02-21-2008 09:32 PM |
| Change Movieclips Variables-Parameters | Jamaican444 | Flash Actionscript Programming | 0 | 01-29-2008 01:16 PM |
| Scope Trouble while calling Functions | aramesh | Flash Actionscript Programming | 1 | 12-14-2007 04:29 AM |
| Play MP3 file using C# | oxygen | C# Programming | 0 | 07-19-2007 11:08 PM |
Our Partners |