Solved

increment through an array from a random index with setInterval()

Posted on 2008-06-24
2
240 Views
Last Modified: 2010-05-18
Hello can anyone help, I'm trying to cycle through an array sequentially, but from a random index.

ie. if index 8 is picked by random then it would go 9, 10, 11 etc..

Ideally I need a setInterval between moving to the next index!

Cheers, P
0
Comment
Question by:Sideas
2 Comments
 
LVL 12

Accepted Solution

by:
SamuelRostol earned 500 total points
ID: 21854396
Hi there,

I'll post three blocks of code, one shows how to find random startindex, the second shows the setInterval-code, and the third contains the actual function :) Look below!

Good luck!
Kindly,
Samuel
/// Array used for testing
var aArray :Array = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k");
 
/// #1 Random Initiater
var iRandomNum = Math.floor(Math.random() * aArray.length); // Finds random index from aArray;
 
/// #2 setInterval
var iIntervalID;
var iIntervalTime :Number = 200; // 200 milliseconds between each trigger
 
iInterval = setInterval(doFunction, iIntervalTime);
 
/// #3 The actual function;
function doFunction()
{
	// Place code here
	trace(aArray[iRandomNum++]);
	
	// Code to end interval when it has looped through the entire interval
	if (iRandomNum >= aArray.length) {
		clearInterval(iInterval);
	}
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:Sideas
ID: 31470067
That's a great solution - you are the man!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question