Reputation: 31
I have some problems with math.random. I made a class for the dices(Disks) and every thing works great. The funny thing is when i trace “var rand” i see that it just picks one number. That makes it totally not random. The reason for this function is that if this random number is above a certain number for example 90, the die(disks) will go to frame 8. But if the random number stays stuck on one number it will never happen. Some help would be nice.
package {
import flash.display.*;
import flash.events.*;
import flash.display.*;
import flash.utils.*;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
public class DiskRoll extends MovieClip
{
var rand:int = Math.random() * 100;
var diskTilt:Timer = new Timer(1350,1);
public function DiskRoll():void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, hitDisk);
diskTilt.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
}
private function hitDisk(event:MouseEvent):void{
this.gotoAndStop(Math.ceil(Math.random()* 7));
if(rand >= 95 && rand <=100){
this.gotoAndStop(8);
}
trace(rand);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, hitDisk);
diskTilt.start();
}
private function onComplete(event:TimerEvent):void {
this.gotoAndPlay(1);
diskTilt.stop();
stage.addEventListener(MouseEvent.MOUSE_DOWN, hitDisk);
}
}
}
Upvotes: 0
Views: 154
Reputation: 5978
rand
is not recomputed in your function, that is why it has always the same value.
I don't get why you gotoAndStop
a random value and keep this rand
variable... You may want to do the following
private function hitDisk(event:MouseEvent):void{
rand = Math.random() * 100;
if(rand >= 95 && rand <=100){
this.gotoAndStop(8);
}
trace(rand);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, hitDisk);
diskTilt.start();
}
These are really basic coding considerations, please read some documentation about OOP and AS3 before trying to code by feeling.
Upvotes: 2