Skip to main content
Participant
July 13, 2012
Answered

Upper/lower limits of Math.random

  • July 13, 2012
  • 1 reply
  • 427 views

Hi,

I currently have applied a random range to my Mouse Cursor. So it fluctuates on the Y axis.

However it obviously takes any value between 0 and 10.

I would like it to either take 0 or 10.  I know you can use math.round / ceil, but does anyone know how I could achieve this?

Thanks.

var mouseposy = mouseY;

          var mouseposx = mouseX;

 

const DURATION:Number = 1;

const VERTICAL_RANGE:Number = 10;

var timer:Timer = new Timer( 50, DURATION * 20 );

timer.addEventListener( TimerEvent.TIMER, onTimer );

timer.addEventListener( TimerEvent.TIMER_COMPLETE, onTimerComplete );

timer.start();

function onTimer( $event:TimerEvent ):void

{

    myCursor.y = mouseposy - (VERTICAL_RANGE * ( Math.random() * 5 ));

          myCursor.x = mouseposx

          stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

          myCursor.visible = true;

}

function onTimerComplete( $event:TimerEvent ):void

{

    x = y = 0;

          stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

}

This topic has been closed for replies.
Correct answer Lemon_Video

try replacing with these lines

const VERTICAL_RANGE:Number = 1

myCursor.y = mouseposy - ((VERTICAL_RANGE * Math.ceil( Math.random() * 2 ))* 10);

1 reply

Lemon_VideoCorrect answer
Inspiring
July 13, 2012

try replacing with these lines

const VERTICAL_RANGE:Number = 1

myCursor.y = mouseposy - ((VERTICAL_RANGE * Math.ceil( Math.random() * 2 ))* 10);