Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

Loop through random numbers

Participant ,
Sep 16, 2015 Sep 16, 2015

I'm trying to spread 8 balls on the stage with an uneven space between them within a range. using  myNum variable in this statement ball.x = 150 + i * myNum; inside for loop I was hopping that it will spread each of the 8 balls in an uneven space. However this is not what happening, instead it position 8 balls with an even space and then about a minute later it positions another set of 8 balls with different spacing.

var minLimit: int = 25;

var maxLimit: int = 43;

var range: int = maxLimit - minLimit;

var myNum: Number = Math.ceil(Math.random() * range) + minLimit;

var balls: Array = [],

  ball: bomb30a;

for (var i: int = 0; i < 8; i++) {

  ball = new bomb30a();

  ball.x = 150 + i * myNum;

  ball.y = 242;

  balls.push(ball);

  addChild(ball);

}

TOPICS
ActionScript
435
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 16, 2015 Sep 16, 2015

use:

var minLimit: int = 25;

var maxLimit: int = 43;

var range: int = maxLimit - minLimit;


function spacingNumF():Number{

return Math.ceil(Math.random() * range) + minLimit;

}

var balls: Array = [],

  ball: bomb30a;

for (var i: int = 0; i < 8; i++) {

  ball = new bomb30a();

  ball.x = 150 + i * spacingNumF();

  ball.y = 242;

  balls.push(ball);

  addChild(ball);

}

Translate
Community Expert ,
Sep 16, 2015 Sep 16, 2015

use:

var minLimit: int = 25;

var maxLimit: int = 43;

var range: int = maxLimit - minLimit;


function spacingNumF():Number{

return Math.ceil(Math.random() * range) + minLimit;

}

var balls: Array = [],

  ball: bomb30a;

for (var i: int = 0; i < 8; i++) {

  ball = new bomb30a();

  ball.x = 150 + i * spacingNumF();

  ball.y = 242;

  balls.push(ball);

  addChild(ball);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 16, 2015 Sep 16, 2015

Perfect. Thank you so much.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 17, 2015 Sep 17, 2015
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines