Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-04-2007, 01:26 AM   #1
mrblifil
Registered User
 
Join Date: Nov 2006
Posts: 16
Red face Random Array Order for Sound Object

Here's the code I have currently which calls any one of three sounds from the library:
Quote:
mySongs = new Array("BGMusic1", "BGMusic2", "BGMusic3");
thisSong = mySongs[Math.ceil(Math.random()*mySongs.length-1)];
myLoop = new Sound(soundclip_mc);
myLoop.attachSound(thisSong);
myLoopVolume = 75;
myLoop.setVolume(myLoopVolume);
myLoop.stop;
myLoop.start(0,999);
This works fine. Every time the user reloads the page, there's a 66.66% chance that the song will be different. The problem is that currently the same tune plays over and over, leaving the other two out completely, unless the user decides to reload, which I don't actually want to have happen anyway. Any thoughts as to how to modify the code so that other two songs will play in succession, and then loop.
mrblifil is offline   Reply With Quote
Old 01-04-2007, 02:30 AM   #2
anonymous
Banned by AS.org Staff
 
Join Date: Oct 2006
Posts: 1,524
Default

myLoop.start(0,999);

Don't loop it, to start with... Use...

myLoop.start(0,1);

And add an onSoundComplete function to your myLoop, so that you attach the next song in the array based on the present index (0,1,2) +1, setting it back to 0, if it's bigger than 2.

Or simply randomize the array at the start of the movie, and have your attached sound cycle through it in that randomised order.

http://www.adobe.com/support/flash/a...ionary664.html
anonymous is offline   Reply With Quote
Old 01-04-2007, 03:30 AM   #3
mrblifil
Registered User
 
Join Date: Nov 2006
Posts: 16
Default

I'll play around with that and let you know how I do. Thanks.
mrblifil is offline   Reply With Quote
Old 01-04-2007, 03:43 AM   #4
anonymous
Banned by AS.org Staff
 
Join Date: Oct 2006
Posts: 1,524
Default

Ÿou're welcome.
anonymous is offline   Reply With Quote
Old 01-05-2007, 03:56 AM   #5
mrblifil
Registered User
 
Join Date: Nov 2006
Posts: 16
Default

Quote:
Originally Posted by anonymous View Post
Or simply randomize the array at the start of the movie, and have your attached sound cycle through it in that randomised order.

http://www.adobe.com/support/flash/a...ionary664.html
This is where I get a bit lost. The array is randomizing at the start of the movie. Based on how my variables are currently named, how do I introduce the concept of adding one to the array value and then going back to zero if the total is greater than 2?
mrblifil is offline   Reply With Quote
Old 01-05-2007, 08:47 PM   #6
J. Grady Broyles
polymorphic by nature
 
Join Date: Dec 2005
Location: Sun Tancisco
Posts: 23
Send a message via AIM to J. Grady Broyles
Default Randomizing an array.

OK. This is a cool thing. A solution I got from a friend.

Here's the summary: You start with an array. Push things into it. Make another empty array tempArray or whatever, Then you generate a random integer 'r' in the range of 0 to the last index of original array. You use array.splice() to take the array index 'r' and push it onto the tempArray.
splice returns an array and actually removes the element from the original array. so you loop until the original array is empty. That's it. You can assign the variable name of of the original to point to the temp array.

Here's the code for creating an array of a deck of cards, each with a value of 0 through 51. And then shuffle it.
var array:Array = new Array(52);
for ( var i:Number = 0; i < 5; i++ )
this.array.push( i );
so now you have an ordered array containing the numbers 0 - 51.

I suggest a shuffle() function. You could call it randomize or whatever. if you have it return an Array, you can just assign the function results right into a variable.

here's my shuffle - pay close attention to array.splice();

public function shuffle():Array
{
var temp_array:Array = new Array(0); // holder for shuffled cards
while ( array.length > 0 )
temp_array.push( array.splice( Math.floor( Math.random() * array.length ), 1 )[0] );
array = temp_array; // optionally copy the reference
return array;
}

OK I know that that single line of code is confusing to look at, but it's an abbreviated way to do several things at once. array.splice() returns an array containing the elements from index n through x indexes.
the signature:

from the AS2 Docs:
splice(startIndex:Number, [deleteCount:Number], [value:Object]) : Array

so, what I'm doing is pushing on to the new array, index 0 of the array returned from splicing a random index with a count of 1. I know how screwy that sounds.
you could do it like this - expanded:

var tempArray:Array = new Array();
while ( array.length > 0 )
{
var randomIndex:Number = Math.floor( Math.random() * array.length ), 1 );
var splicedArray:Array = array.splice( randomIndex, 1);
var item:Number = splicedArray[0];
tempArray.push( item );
}
J. Grady Broyles is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stuffing an Object into a Shared Object dvlnblk ActionScript 2.0 3 08-07-2007 03:41 PM
putting an object inside an array and then calling a method inside the object kensuguro ActionScript 2.0 3 02-08-2006 04:21 PM
Array and duplicate random pictures wilmax Other Flash General Questions 7 12-08-2005 09:38 PM
Play Movies in a random order lk_ ActionScript 2.0 0 10-18-2005 12:34 PM


All times are GMT. The time now is 01:23 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.