24 July 2009

ActionScript Random Number Generator With Blur Effect

There was a few posts on my older blog in form of tutorial on how to create random number generator with blur effect. I'm near bandwidth limit, so no live example, but click on preview image to download files.

Flash random number generator


here is required code



// author: FLANTURE http://flanture.blogspot.com
// date: 24 Jan 2009

import flash.filters.BlurFilter;

// function for roll_mc movie clip onPress action. If timer has not started,
// that means there is no action in progress, so we can start one.

roll_mc.onPress = function() {
if (!timerStarted) {
var filter:BlurFilter = new BlurFilter(60, 120, 1);
var filterArray:Array = new Array();
filterArray.push(filter);
num1_txt.filters = filterArray;
var newNumber1 = Math.floor(Math.random()*10);
num1_txt.text = newNumber1;
num2_txt.filters = filterArray;
var newNumber2:Number = Math.floor(Math.random()*10);
num2_txt.text = newNumber2;
num3_txt.filters = filterArray;
var newNumber3:Number = Math.floor(Math.random()*10);
num3_txt.text = newNumber3;
timerStarted = true;
}
}

// functions for restoring blur effect

function restoreBlur1() {
num1_txt.filters = new Array(filter);
}
function restoreBlur2() {
num2_txt.filters = new Array(filter);
}
function restoreBlur3() {
var filter:BlurFilter = this.filters[0];
filter.blurY = 0;
filter.blurX = 0;
num3_txt.filters = new Array(filter);
}

// next function controls when blur should stop and numbers should display.

onEnterFrame = function () {
if (timerStarted) {
if (counter < thirdDigitStop) {
counter += 1;
if (counter == firstDigitStop) {
restoreBlur3();
}
if (counter == secondDigitStop) {
restoreBlur2();
}
} else {
timerStarted = false;
counter = 0;
restoreBlur1();
}
}
}

// you can change firstDigitStop and other variables to suit your needs.
// numbers represent frames, not seconds.

onLoad = function () {
timerStarted = false;
counter = 0;
firstDigitStop = 8;
secondDigitStop = 16;
thirdDigitStop = 24;
}



This example can be used as slot machine simulation or anything similar you can think of.

*_*

9 comments:

aileen said...

I recently came accross your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.


Susan

http://dclottery.info

Jim Johnson said...

Thanks for the sample code. Whenever I load the FLA into Flash, then export to SWF (even when changing nothing), the blur effect is gone. Am I doing something wrong? I'm using Flash CS4. Thank you.

Anonymous said...

Hi Jim, code is actually as2, I'm not sure if it works with CS4?! but it's easy to rewrite it in as3

jim said...

Its a useful actionscript.

I am a high school student from Hong Kong.
I would like to ask you a question:

What if i want to make a new button named "roll_mc2" and generate the other three digits near it? (named-num4_txt,num5_txt and num6_txt)

I try it myself but why the initial three digits won't stop and keep blur?

// author: FLANTURE http://flanture.blogspot.com
// date: 24 Jan 2009

import flash.filters.BlurFilter;

// function for roll_mc movie clip onPress action. If timer has not started,
// that means there is no action in progress, so we can start one.



roll_mc1.onPress = function() {
if (!timerStarted) {
var filter:BlurFilter = new BlurFilter(60, 110, 1);
var filterArray:Array = new Array();
filterArray.push(filter);
num1_txt.filters = filterArray;
var newNumber1 = Math.floor(Math.random()*10);
num1_txt.text = newNumber1;
num2_txt.filters = filterArray;
var newNumber2:Number = Math.floor(Math.random()*10);
num2_txt.text = newNumber2;
num3_txt.filters = filterArray;
var newNumber3:Number = Math.floor(Math.random()*2);
num3_txt.text = newNumber3;
timerStarted = true;
}
}




// functions for restoring blur effect

function restoreBlur1() {
num1_txt.filters = new Array(filter);
}
function restoreBlur2() {
num2_txt.filters = new Array(filter);
}
function restoreBlur3() {
var filter:BlurFilter = this.filters[0];
filter.blurY = 0;
filter.blurX = 0;
num3_txt.filters = new Array(filter);
}

// next function controls when blur should stop and numbers should display.

onEnterFrame = function () {
if (timerStarted) {
if (counter < thirdDigitStop) {
counter += 1;
if (counter == firstDigitStop) {
restoreBlur3();
}
if (counter == secondDigitStop) {
restoreBlur2();
}
} else {
timerStarted = false;
counter = 0;
restoreBlur1();
}
}
}

// you can change firstDigitStop and other variables to suit your needs.
// numbers represent frames, not seconds.

onLoad = function () {
timerStarted = false;
counter = 0;
firstDigitStop =10;
secondDigitStop = 15;
thirdDigitStop = 20;

}


roll_mc2.onPress = function() {
if (!timerStarted2) {
var filter:BlurFilter = new BlurFilter(60, 110, 1);
var filterArray:Array = new Array();
filterArray.push(filter);
num4_txt.filters = filterArray;
var newNumber1 = Math.floor(Math.random()*10);
num4_txt.text = newNumber1;
num5_txt.filters = filterArray;
var newNumber2:Number = Math.floor(Math.random()*10);
num5_txt.text = newNumber2;
num6_txt.filters = filterArray;
var newNumber3:Number = Math.floor(Math.random()*2);
num6_txt.text = newNumber3;
timerStarted2 = true;
}
}

// functions for restoring blur effect

function restoreBlur1() {
num4_txt.filters = new Array(filter);
}
function restoreBlur2() {
num5_txt.filters = new Array(filter);
}
function restoreBlur3() {
var filter:BlurFilter = this.filters[0];
filter.blurY = 0;
filter.blurX = 0;
num6_txt.filters = new Array(filter);
}

// next function controls when blur should stop and numbers should display.

onEnterFrame = function () {
if (timerStarted2) {
if (counter < thirdDigitStop) {
counter += 1;
if (counter == firstDigitStop) {
restoreBlur3();
}
if (counter == secondDigitStop) {
restoreBlur2();
}
} else {
timerStarted2 = false;
counter = 0;
restoreBlur1();
}
}
}

onLoad = function () {
timerStarted2 = false;
counter = 0;
firstDigitStop =10;
secondDigitStop = 15;
thirdDigitStop = 20;

}

flanture said...

hi Jim,

I didn't have time to answer your question, can you e-mail me with details : flanture [at] gmail [dot] com ?

I would like to help if I can
Flanture

Anonymous said...

Hi Jim:
What should i change if I want to limit the random number to 200?.
Appreciate.
Best regard.
David

Clint said...

Hi there!

I really like your blog. This code has saved me a lot of time for whipping up some raffle application for a Christmas party in our church organization. :)

Btw, I modified the code to do some additional stuff, and posted it in my blog (and showed an example). This might be useful to other people as well.

http://www.ostalks.com/2010/12/24/a-modified-flash-random-number-generator/

Will be looking forward to more of your blog posts!

Happy holidays!

flanture said...

Hi Clint, Happy Holidays!

I really appreciate you found my code useful and even more you have extended it! Your modification is great and visually is amazing.

Thanks again,
Flanture

Anonymous said...

"blur effect could not be loaded"
using adobe flash professional cs5.5
and dude you should write that its AS2.. since many people don't know that..
Any suggestions?
Thanks.

 

template by blogger templates