IT Community - Software Programming, Web Development and Technical Support

Trouble getting movieclips to play at random

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Flash Actionscript Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 02-07-2008, 05:44 AM
aramesh aramesh is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 72
aramesh is on a distinguished road
Default Trouble getting movieclips to play at random

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 02-11-2008, 12:30 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 632
oxygen is on a distinguished road
Default Re: Trouble getting movieclips to play at random

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...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 02-11-2008, 08:25 PM
aramesh aramesh is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 72
aramesh is on a distinguished road
Default Re: Trouble getting movieclips to play at random

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 02-14-2008, 03:41 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 632
oxygen is on a distinguished road
Default Re: Trouble getting movieclips to play at random

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...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 02-15-2008, 12:47 AM
aramesh aramesh is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 72
aramesh is on a distinguished road
Default Re: Trouble getting movieclips to play at random

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 02-19-2008, 05:48 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 632
oxygen is on a distinguished road
Default Re: Trouble getting movieclips to play at random

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&lt;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&gt;=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...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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


All times are GMT -7. The time now is 11:43 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0