View Full Version : Random Code
Ravmaster
May 9th, 2006, 02:49 PM
number=Math.ceil(Math.random()*2);
if(number==1){
_root.baddy.gotoAndStop(5);
}
if(number==2){
_root.baddy.gotoAndStop(3);
}
can someone tell me whats wrong with this code please...
InfestedDemon
May 9th, 2006, 03:42 PM
typo maybe?
Nich
May 9th, 2006, 04:40 PM
Have you tried tracing your random ouput to see if you are in fact only getting 1's and 2's? I know the code is designed that way but flash can be wierd about these things
nathan99
May 9th, 2006, 06:06 PM
can't you just use the random() function instead to get whole numbers?
number=random(2)+1;
if(number==1){
_root.baddy.gotoAndStop(5);
}
if(number==2){
_root.baddy.gotoAndStop(3);
}
optixburn
May 10th, 2006, 06:23 PM
number=Math.ceil(Math.random()*2);
if(number==1){
_root.baddy.gotoAndStop(5);
}
if(number==2){
_root.baddy.gotoAndStop(3);
}
can someone tell me whats wrong with this code please...
don't use number ... flash might be reading it as a reserved word
also to note that this can still produce a 0 output so that should be taken into consideration.
try this
myNum = Math.ceil(Math.random()*2);
if(myNum == 1){
_root.baddy.gotoAndStop(5);
}else if(myNum == 2){
_root.baddy.gotoAndStop(3);
}else{
// default
_root.baddy.gotoAndStop(3);
}
if this still doesn't work make sure you are linking correctly
_root.any_kind_of_reference_of_a_movieClip._proper ty;
this seems to be a big sourcce of a lot peoples problems not knowing how pathing works
hope this helps
SNOWmanx
May 14th, 2006, 06:42 PM
You could always go with the classic randRange they gave out in the Help Files.
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
so all you have to do is...
number=randRange(1,2);
if(number==1){
_root.baddy.gotoAndStop(5);
}
if(number==2){
_root.baddy.gotoAndStop(3);
}
Ravmaster
May 16th, 2006, 01:56 PM
also now this code dont work :'(
_root.main.main.gotoAndStop(1)
Joppe
May 16th, 2006, 02:28 PM
Why are you using math . ceil?
number=Math.random(2)
Math.round(number)
if(number==1){
_root.baddy.gotoAndStop(5);
}
if(number==2){
_root.baddy.gotoAndStop(3);
}
whats wrong with that?
Ophyr
May 16th, 2006, 03:59 PM
He's using Math.ceil because random doesn't generate 1's and 2's, but numbers with fractions like 0.12342 (Which will become 1 if you 'ceil' it :P)
Just a little cosmetic, but wouldn't a switch statement look alot better ? :P
The only reasons I can think of that make your code NOT work are these :
If you trace 'number' and it's undefined, flash probably wants you to assign a value to number before you use it like
var number:Number = 0;
number = Math.ceil(Math.random()*2);
Other reason could be a naming issue with your movie clip. Try tracing the clip to see if it really exists :)
switch(Math.ceil(Math.random()*2)){
case 1 :
_root.baddy.gotoAndStop(3);
break;
case 2 :
_root.baddy.gotoAndStop(5);
break;
}
Ravmaster
May 16th, 2006, 04:05 PM
ok done that bit works fine now thnx everyone i just need to know why this code dont work,,,
_root.main.main.gotoAndStop(1)
Ophyr
May 17th, 2006, 03:36 AM
Is there a 'main' clip inside a 'main' clip in your root :P Check the instance names I'd say, or do a trace(_root.main.main); to see wether the clip you're accessing exists. You did miss a ; at the end of that line but I'm not sure if Flash is too picky about that :P
Joppe
May 17th, 2006, 10:09 AM
^ Math.random(2) returns an decimal number but you can round it
Math.round() or you could just use random(2) without the Math. thats returns just whole numbers.
Ravmaster
May 17th, 2006, 02:08 PM
i traced the thing and it was found but it still didnt work
Joppe
May 17th, 2006, 03:49 PM
Rav, now it has come to the point when you post your fla..
Ophyr
May 17th, 2006, 05:04 PM
^ Math.random(2) returns an decimal number but you can round it
Math.round() or you could just use random(2) without the Math. thats returns just whole numbers.
You're right, I always use Math.random() * value -- thanks :D
Ravmaster
May 18th, 2006, 07:55 AM
Rav, now it has come to the point when you post your fla..
hehe i guess your right :D
http://www.verzend.be/v/6263868/Main.fla.html is that an ok host?
Joppe
May 18th, 2006, 01:59 PM
Ok i'll look at it
Ravmaster
May 19th, 2006, 05:01 PM
thankies ;)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.