Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ActionScript Code Wanted for Random "Flash Cards" 2

Status
Not open for further replies.

bsquared18

Technical User
Joined
Jun 10, 2001
Messages
329
Location
US
Let's say on one layer I have four frames, each containing different content. On another layer, I have a button. Each time the user presses the button, I want them to be taken randomly to one of the four frames. The next button push, they are randomly taken to another frame, without repeating a frame already seen.

This goes on until all four frames have been seen. On the fifth button push, the user is sent to a fifth frame that has the message "Finished."

Could someone tell me the button.onRelease ActionScript code to accomplish this? Either AS 1 or AS 2, but please tell me which is being used.

Thanks!
Bill B.
 
as1

something like

frames =[1,2,3,4];
myButton.onRelease = function(){
myRandom = random(frames.length);
gotoAndStop(myrandom);
frames.splice(myrandom,1);
if(frames.length==0)
gotoAndStop("finished");
}

ought to do the job


code untested....just off the top of my head
 
Well, this is basically the same as Bill's but in AS2.

You need 6 frames - the 1st frame is "Start" and the 6th frame is "Finished". Frame 2, 3, 4, 5 are your 4 frames where you have 4 different contents.

Place a button in a layer across frame 1 to 5, and name it "buttonX" for now.

Create a layer for the script and place the following script in the frame 1:

[tt]// ActionScript 2
var frames:Array = [2, 3, 4, 5];
stop();
//
this.buttonX.onPress = function() {
var frameNumber:Number;
var n:Number = this._parent.frames.length;
if (n) {
var randomIndex:Number = Math.floor(Math.random()*n);
frameNumber = this._parent.frames.splice(randomIndex, 1);
} else {
frameNumber = 6;
}
this._parent.gotoAndStop(frameNumber);
};
//[/tt]

Kenneth Kawamoto
 
Thanks, Bill and Kenneth!

Bill, I couldn't get yours to work, probably because of my lack of ActionScript knowledge. Kenneth, yours fit the bill perfectly.

Bill B.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top