I have eight movie clips on my main timeline. The idea is to play the first movie clip, once a button is clicked, go to a random frame 2-8 on the timeline and play the movielip on that frame, the get sent back to frame 1 to start the process over. I have this figured out, but I also don't want any frames repeating until they have all been played. Inside the first movieclip is a button with the following actionscript attached. Everything works perfectly EXCEPT my frames are repeating before they have all played. I can't see in my script where I have gone wrong. Please help!
var nums:Array = new Array();
1,2,3,4....
for(var i:uint=1; i<7; i++){
nums.push(i);
}
// for shuffling the array
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
// shuffle the array
shuffle(nums);
// set up the controls for the button
var count:uint = 0;
boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);
function plyRndmMv(evt:MouseEvent):void {
MovieClip(root).gotoAndStop(nums[count]+2);
count++;
if(count == 8){
shuffle(nums);
count = 0;
}
}
var nums:Array = new Array();
1,2,3,4....
for(var i:uint=1; i<7; i++){
nums.push(i);
}
// for shuffling the array
function shuffle(a:Array):Array {
var randomArray=new Array();
var p:int;
var t:*;
var ivar:int;
while(a.length>0){
p=Math.round(a.length-1*Math.random());
randomArray.push(a[p])
a .splice(p, 1);
}
return randomArray;
}
// shuffle the array
shuffle(nums);
// set up the controls for the button
var count:uint = 0;
boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);
function plyRndmMv(evt:MouseEvent):void {
MovieClip(root).gotoAndStop(nums[count]+2);
count++;
if(count == 8){
nums=shuffle(nums);
count = 0;
}
}
use:
var alreadyExecuted:Boolean;
if(!alreadyExecuted){
alreadyExecuted=true;
var nums:Array = new Array();
1,2,3,4....
for(var i:uint=1; i<7; i++){
nums.push(i);
}
// for shuffling the array
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
// shuffle the array
shuffle(nums);
// set up the controls for the button
var count:uint = 0;
boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);
function plyRndmMv(evt:MouseEvent):void {
MovieClip(root).gotoAndStop(nums[count]+2);
count++;
if(count == 8){
shuffle(nums);
count = 0;
}
}
}
it is assigned to true if you copied that code.
what do the traces reveal:
var alreadyExecuted:Boolean;
if(!alreadyExecuted){
alreadyExecuted=true;
var nums:Array = new Array();
//1,2,3,4....
for(var i:int=2; i<=8; i++){
nums.push(i);
}
// for shuffling the array
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
// shuffle the array
shuffle(nums);
trace(nums);
// set up the controls for the button
var count:uint = 0;
boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);
function plyRndmMv(evt:MouseEvent):void {
MovieClip(root).gotoAndStop(nums[count]);
trace(nums[count]);
count++;
if(count == 8){
shuffle(nums);
count = 0;
}
}
}
I've uploaded a smaller version of my fla file here, If you want to look at it:
http://www.lenapodesta.com/bb_master_v18_hlp.fla
The code that we are talking about is in the movieclip on the first frame(startPage), on frame 1
I put the code and animation on my main timeline. This pushed the frames with the movieclips on them back to frames 359-365. My actionScript looks like this"
var alreadyExecuted:Boolean;
if(!alreadyExecuted){
alreadyExecuted=true;
var nums:Array = new Array();
//1,2,3,4....
for(var i:int=359; i<=365; i++){
nums.push(i);
}
// for shuffling the array
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
// shuffle the array
shuffle(nums);
trace(nums);
// set up the controls for the button
var count:uint = 0;
boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);
function plyRndmMv(evt:MouseEvent):void {
MovieClip(root).gotoAndStop(nums[count]);
trace(nums[count]);
count++;
if(count == 7){
shuffle(nums);
count = 0;
}
}
}
On trace, my output looks like this
365,360,363,364,361,359,362
365
This is correct. However, when the start page animation starts up again and I try to click on the buttone to play the next frame in the array, nothing happens. Any suggestions?
if your button is being reinitialized, you need to re-execute its listener code. ie, remove boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv); from the if(!alreadyExecuted) block:
var alreadyExecuted:Boolean;
boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);
if(!alreadyExecuted){
alreadyExecuted=true;
var nums:Array = new Array();
//1,2,3,4....
for(var i:int=359; i<=365; i++){
nums.push(i);
}
// for shuffling the array
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
// shuffle the array
shuffle(nums);
trace(nums);
// set up the controls for the button
var count:uint = 0;
function plyRndmMv(evt:MouseEvent):void {
MovieClip(root).gotoAndStop(nums[count]);
trace(nums[count]);
count++;
if(count == 7){
shuffle(nums);
count = 0;
}
}
}
North America
Europe, Middle East and Africa
Asia Pacific