13 Replies Latest reply on Jun 29, 2009 2:36 PM by Arithian

# [MX04][AS2]moving to random frame

Hello,
currently I have been encountering a problem with random frame movement.I want to do something like this-
When I come to frame 1, automatically go to frame 2 or 15, not 2-15,
ONLY 2 OR 15.
I know how to do 2-15,
gotoAndPlay(random(2)+15)
Can somebody help me!
Thank You,
Chinmaya
• ###### 1. Re: [MX04][AS2]moving to random frame
gotoAndPlay(13*random(2)+2);

or in proper AS2 way,

gotoAndPlay(13*Math.floor(2*Math.random())+2);
• ###### 2. Re: [MX04][AS2]moving to random frame
Thank You!
That worked!
But can you still explain the script to me as I could then use it for differnet frame numbers also!
once more, thanks,
Chinmaya
• ###### 3. Re: [MX04][AS2]moving to random frame
random(2)
this give you answer either 0 or 1

13*random(2)
this give you answer either 13*0 or 13*1, means 0 or 13

13*random(2)+2
this give you answer either 0+2 or 13+2, means 2 or 15
• ###### 4. Re: [MX04][AS2]moving to random frame
above is just one of many different ways to solve your problem,
you must understand what you need, & think logical way to get it.
In your case, you need to get a number, either 2 or 15.

beside that, below is some other way to get what you need.

2nd solution, using if/else statement
if(random(2)){
gotoAndPlay(2);
} else {
gotoAndPlay(15);
}

3nd solution, same logic with 2nd solution, but use ?: statement
random(2)?gotoAndPlay(2):gotoAndPlay(15);
• ###### 5. Re: [MX04][AS2]moving to random frame
4th solution
gotoAndPlay(random(2)?2:15);
• ###### 6. Re: [MX04][AS2]moving to random frame
Hello,
Thank You very much! The best part was the explaination to the solution.Well, now I can get back to work on my project.
And yes, if you have the time and are not too busy can you tell me what the ?: does.I am still not an advanced coder but still have made some small games using actionscript.Also, I used the if(!Key.isDown()){} command for no button pressed and it worked.Can you explain all this.
THANK YOU,
SINCERELY,
CHINMAYA

PS- I am not in a rush so please if you are busy, take your time.
• ###### 7. Re: [MX04][AS2]moving to random frame
Sure I have time, I have 24 hours of time each day

when i say
a?b:c
it means
if(a){ b; } else { c; }

can you understand this?
• ###### 8. Re: [MX04][AS2]moving to random frame
and for if(!Key.isDown()){}, do you understand the meaning of ! sign?
• ###### 9. Re: [MX04][AS2]moving to random frame
Hello,
for the if(!Key.isDown(.............. one, I know the working, I just need to know the meaning of the ! sign and how can it make the code work because without it, flash gives an error!
and I couldn't understand the ?! one.
a?b:c
means
if(a){ b; } else { c; }
gotoAndPlay(random(2)?2:15);
I got this- else {c;} means that if (a){b;) doesn't work then goto c.
but what does (a){b;} mean?
I know that random(2) means randomly choose 0 or 1.
NOPE, CANT FIGURE IT ouT!
Thanks for the reply,
Chinmaya
• ###### 10. Re: [MX04][AS2]moving to random frame
! means NOT

when we say
a=1;
b=2;
if(a==b){
//codes write here will not run, because a is not equal to b
}

when we say
a=1;
b=2;
if(a!=b){
//codes write here will run
}
• ###### 11. Re: [MX04][AS2]moving to random frame
gotoAndPlay(random(2)?2:15);

for random(2),
it gives either 0 or 1,

and for random(2)?2:15,
when random(2) is 1, it return 2,
when random(2) is 0, it return 15,

so, gotoAndPlay(random(2)?2:15); will gotoAndPlay either 2 or 15.

• ###### 13. Re: [MX04][AS2]moving to random frame

I know I am bumping a really old post but I am wondering how to do this where you would randomly go to one of multiple frames?  Lets say frames 40, 90, 140, 190, and so on.  Any ideas?