0

I'm trying to create a guessing game that has the user clicking on different colored boxes to see which one is correct. I have a public var that dictates which color(later image) to use. The code is this in my update:

        if (FlxG.mouse.justPressed())
    {

        block2.distributionp = Math.random() * 2;
        block2.colorArray = block2.distributionp;
        block2.colorUnit = block2.colorArray;

    }

(colorUnit and colorArray both equal distributionp, which is a ranom of 2 in the class file)

When I run this code, the change does occur, but it only seems to switch out once. The other times it's ignored. How can I get this to continuously switch out a random number that I can use later?

Thanks in advance!

xhunterko
  • 49
  • 7
  • 2
    Are you positive this code is getting hit each time? Have you put a trace in there to verify that the code here is executing every click ? If you know how to debug, you might want to put a breakpoint in there. – prototypical Jan 02 '13 at 04:13
  • If I'm reading this correctly, you are setting all three of those to the exact same value, correct? Is there a purpose for that? Or do you want all three of them to all be chosen randomly and set to different random values? – IQAndreas Jan 02 '13 at 09:17
  • Also, when do you want it to "continuously switch out a random number"? Every frame? Every time the user clicks something? – IQAndreas Jan 02 '13 at 09:21

2 Answers2

0

Math.random() * 2 Returns a Number ranging from 0.000000000000 to 2.00000000, this includes numbers like 0.123456789 and 1.99999999 (Not sure exactly what decimal place it goes to but just saying it doesn't only return integers 0, 1 and 2). I'm not sure but I think you're problem lies in the range, so if you want a better range use this code.

MIN_VALUE * Math.random() + (MAX_VALUE - MIN_VALUE);

If you would like to only get integers you can use either Math.ceil() or Math.floor() like so :
Math.ceil(MIN_VALUE * Math.random() + (MAX_VALUE - MIN_VALUE));

I'm sorry if this does not help you but let me know if it doesn't and I will continue to try and help.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
  • No. See, the problem isn't in the value of the number. The problem is trying to set it. I'm not sure if it's setting correctly or not. Thanks though. – xhunterko Jan 02 '13 at 01:41
  • Oh ok, question, is block2.colorArray an Array, or basically what is block2.colorArray? Also what objects or primitives are block2.distributionp and block2.colorUnit? – Jonny Henly Jan 02 '13 at 01:44
  • block2.colorArray is an int. Did'nt know how to get it to work as an array. – xhunterko Jan 02 '13 at 01:49
  • When you call Math.random() * 2 do you want an integer between 0 and 2 returned or do you want a floating point number? – Jonny Henly Jan 02 '13 at 01:52
0

The value I'm getting is whole integers between 0 and 2. What I'm trying to do, is change the vaue for each time I click. I'm not sure what occurs. Perhaps I shouldn't have asked now. I'll ask later when I have more info. Sorry.

xhunterko
  • 49
  • 7