Expand my Community achievements bar.

SOLVED

dice game random numbers

Avatar

Level 3

Hi,

Let's say I have five dice...like in a game. I want to click a button that sets a random value (1thru 6) for each die.

I put the  dice into a group called dice.

How do I address a specific die? Is it like an index or a node or an instance? Something like that?

for (i=0; i<=4; i++)

{

var die = Math.floor(Math.random()*6);

dice.index??? = die

But I want the dice.index??? to takke on the value of the loop so that I can set the value for each die of 5.

}

In other words, I do not want to write the repetitive code:

var die = Math.floor(Math.random()*6);

die1.rawValue = die + 1;

var die = Math.floor(Math.random()*6);

die2.rawValue = die + 1;

var die = Math.floor(Math.random()*6);

die1.rawValue = die + 1;.rawValue = die + 1;

var die = Math.floor(Math.random()*6);

die1.rawValue = die + 1;.rawValue = die + 1;

et cetera

Many thanks,

Joe

1 Accepted Solution

Avatar

Correct answer by
Level 10

The following will do the trick.

All the fields need to have the same name so that they get an instance number: [0], [1], etc. I've called the fields "DieRoll".

for (i=0; i<=4; i++)

{

     var die = Math.floor(Math.random()*6) + 1;

     xfa.resolveNode("DieRoll[" + i + "]").rawValue = die;

}

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

The following will do the trick.

All the fields need to have the same name so that they get an instance number: [0], [1], etc. I've called the fields "DieRoll".

for (i=0; i<=4; i++)

{

     var die = Math.floor(Math.random()*6) + 1;

     xfa.resolveNode("DieRoll[" + i + "]").rawValue = die;

}

Avatar

Level 3

Perfect Jono!!! Thank you.

I know this wasn't difficult, but it was for me.

This is much easier than what I was thinking. And now I understand how to use those instance numbers better.

Thanks again,

Joe