[FMX] Help working out/learning lottery script

Hey all,

Recently I started a thread about a lottery draw and got a good response that helped me out thanks to Adam14 and Kode.

http://www.kirupaforum.com/forums/showthread.php?t=52438
[url=“http://www.kirupaforum.com/forums/showthread.php?t=52438”]
Kode gave a basic file that included a random number draw and a ‘bank’ that payed out if lotto balls were correct.

My problem is the script is rather advanced and just passes over my head and I am still an newbie at action script, I would be very grateful if anyone could help explain/comment the script. User ‘Futureflash’ has added some parts.

The basic file is attached and script is below.
[url=“http://www.kirupaforum.com/forums/newthread.php?do=newthread&f=9”]
[color=black][font=Verdana]
[/font][/color][color=black][font=‘Trebuchet MS’]function resetLotto(reset)[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]{for (var prop in output) [/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]{output[prop].text = “?”;[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]status.htmlText = “<p align=“center”><a href=“asfunction:playLotto”>Click here to play!</a></p>”;[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]clearInterval(id);[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]function playLotto() {[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]var prop, numbers, index, ran; ‘variables[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]Message.text = “”; ‘clears the message box from any message displayed out[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]for (prop in input) { [/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]if (!input[prop].length) { [/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]message.htmlText = “<p align=“center”>Enter a number in all the fields!</p>”; ‘ Displays out message, why htmlText[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]id = setInterval(resetLotto, interval); ’[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]return; ‘not sure, going by the message a check to ensure all input boxes have values[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]numbers = []; [/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]for (index=1; index<=maxvalue; index++) { [/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]numbers.push(index); ‘ not sure[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]for (index=0; index<output.length; index++) { [/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]ran = Math.floor(Math.random()*numbers.length); [/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]output[index].text = numbers[ran]; ‘not sure[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]numbers.splice(ran, 1); ‘ not sure[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]for (index=0; index<output.length; index++) {[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]if (input[index].text != output[index].text) {[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]status.htmlText = “<p align=“center”>You’ve lost!</p>”; 'Displays out loosing message, any reason for html??[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]id = setInterval(resetLotto, interval);[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]return;[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]Message.htmlText = “<p align=“center”><b>You’ve won!</b></p>”; displays out winning message,any reason for html??[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]var id, interval = 2000; ‘ Variables and sets the interval to 2 seconds ???[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]var maxvalue = 49; ‘Set the maximum value[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]var output = [output0, output1, output2, output3, output4, output5]; ‘ Sets variables out for the text boxes[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]var input = [input0, input1, input2, input3, input4, input5]; ‘ Sets variables out for the text boxes[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]for (var prop in input) {[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]input[prop].restrict = “0-9”;[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]input[prop].onChanged = function() {[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]var n = parseFloat(this.text);[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]if (!isNaN(n) && n>maxvalue) {[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]this.text = maxvalue;[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]};[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]}[/font][/color][color=black][font=‘Trebuchet MS’]

[/font][/color][color=black][font=‘Trebuchet MS’]resetLotto();[/font][/color][color=black][font=Verdana][/font][/color]

[font=Times New Roman][size=3] [/size][/font]

anybody know about this script, it would be a great help.

Pretty cool little game. I am working on commenting it so give me a few . . .

:hr:

Here ya go. If you still dont’ understand some of the code

  1. look stuff up in the actionscript reference in Flash
  2. ask more questions here!

:hr:

Lunatic you are a ledgend,

I hoped someone would help me out a little but that’s brilliant.
I will slowly…slowly go thru the script and work out in my head the various parts.

Thanks a million for taking the time to help me out, the game is cool but I didnt understand the script workings, so now I can sleep at night…lol

lol, no problem daboz. Good luck getting through it - there was one line of code that I didn’t get. I’m gonna post it here and hopefully someone (HEY SCOTTY! HINT HINT) will jump on it and translate it for us . . .

:hr:

Okay here it is, and it is within a great big for loop:


for (index=0, total=0; index<output.length; index++) {
//yadda yadda yadda
**total += input[index].text == numbers[ran];**
//yadda yadda yadda
}

total = total + input[index].text but only when input[index].text is (comparatively) equal to numbers[ran]?

Is this like a shorthand if statement? Does anyone know?

total = a variable that tracks the number of correct guesses
input[index] = the array of numbers input by the user
numbers[ran] = the array of randomly generated numbers created by the program
output = an array of dynamic textboxes that display the randomly generated numbers

I heard a princess shout for help, so I took my horse to find what was wrong=)

total += input[index].text == numbers[ran];

I think you’re right (I’m not completely sure) that it’s a sort if statement:
if numbers[ran] (the random number) equals the input number, so is true (equals 1) , add 1 to total.
Hope this makes sense;)

scotty(-:

Thanks so much Scotty!!

:bu:

welcome=)