View Full Version : Going to a random frame
ChaosEmerl
07-31-2006, 07:22 AM
Basically, what I want is, on the click of a button, the movie to go to a random frame. Here's what I have:
on (release) {
var say;
say = random(4);
if (say = "0") {
gotoAndStop(2);
}
else if (say = "1") {
gotoAndStop(3);
}
else if (say = "2") {
gotoAndStop(4);
}
else if (say = "3") {
gotoAndStop(5);
}
}
That seems like it should work, but it doesn't. It always goes to frame 3, no matter what I set the variable as. Is there something I'm missing here? Am I going about this the completely wrong way?
FormerSwinger
07-31-2006, 08:51 AM
HI
There's a couple of things that cause your script to fail. First the random number is not a String so lose the quotes. Second the you are not assigning a new value so add a second = sign.
I'd do like this:
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.round(Math.random()*(max-min))+min;
return randomNum;
}
on (release) {
var say;
say = randRange(2,5);
gotoAndStop(say);
}
ChaosEmerl
07-31-2006, 08:42 PM
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.round(Math.random()*(max-min))+min;
return randomNum;
}
on (release) {
var say;
say = randRange(2,5);
gotoAndStop(say);
}
Am I supposed to replace "number" with anything? Because that script doesn't work. Might note that I'm using Flash MX (6) so some of those commands might not be in that version.
FormerSwinger
08-01-2006, 11:16 AM
Don't know about version six. Try removing the variable types:
function randRange(min, max){
var randomNum= Math.round(Math.random()*(max-min))+min;
return randomNum;
}
on (release) {
var say;
say = randRange(2,5);
gotoAndStop(say);
}
ChaosEmerl
08-02-2006, 02:32 AM
Thanks. Worked perfectly. Just had to move the "on (release) {" to the beginning.
barisbc
12-05-2009, 01:09 AM
Hello, I am trying to do the same thing, but the code isn't working for me. I am using this code:
on (release) {
function randRange(min, max){
var randomNum= Math.round(Math.random()*(max-min))+min;
return randomNum;
}
on (release) {
var say;
say = randRange(2,5);
gotoAndStop(say);
}
By the way, the function is supposed to associate with the button on the same frame right?
Renderer10
12-05-2009, 05:34 AM
First of all, is your code on a button or on the timeline? Either way, remove the on release around the randRange function. If your code is on the timeline, then for simplicity's sake move it to the button.
I don't like the randRange function here at all. It's not going to pick the minimum number or maximum number as much as the numbers in between. Try this:
Math.floor(Math.random()*(max-min+1))+min
-Renderer10
barisbc
12-05-2009, 07:00 AM
Hey, thanks for your answer. I did what you said, its working perfectly with one exception. I moved the code to the keyframe where the button is. This is on the 2nd frame. I have another button on the first frame that goes to the 2nd frame. I would expect (and want) it to go to the random frame when the button on the second frame is clicked. But when I click the button on the first frame to go to the 2nd frame, the random frame is already there. English isn't my first language, so sorry if there is anything unclear.
barisbc
12-07-2009, 11:07 AM
Nevermind, I got it. Just added these codes on top:
instanceName.addEventListener(MouseEvent.CLICK, gotoAndStopAnimation);
function gotoAndStopAnimation(event:MouseEvent):void
{
function randRange(min, max){
var randomNum= Math.floor(Math.random()*(max-min+1))+min;
return randomNum;
}
var say;
say = randRange(30,35);
gotoAndStop(say);
}
Renderer10
12-07-2009, 12:11 PM
Yeah that'll work... before you were using an AS2 style of detecting clicks, but you were using it on the timeline when it should have been on the button itself, the addEventListener method is much better. Your code could still be cleaned up a bit, as you're declaring unecessary variables. The randRange function could be declared globally too, like this:
function gotoAndStopAnimation(event:MouseEvent):void {
gotoAndStop(randRange(30,35));
}
function randRange(min, max) {
return (Math.floor(Math.random()*(max-min+1))+min);
}
barisbc
12-08-2009, 07:32 PM
thank you! before I posted my question I didn't even know if I was using AS3 or AS2.
actionman2011
11-28-2011, 09:10 PM
Hi,
How do I get this to work in AS3 on the timeline or in a movie clip?
Kay
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.