13 April 2010

Random Generator updated to AS3

If you are blogger, are you happy to see your older posts are still read and commented, even years has past? I am. Especially if I see ppl are using my older code for learning. That's why I updated AS2 Random Number Generator to AS3 version.

There is one new getRan function which return random number less than max parameter. You can use this to limit the number you want to display. Here is new code with minor changes.


// author: FLANTURE http://flanture.blogspot.com
// date: 24 Jan 2009
// updated: 13 Apr 2010 (AS2 -> AS3)

import flash.filters.BlurFilter;
import flash.events.Event;
import flash.text.TextField;

var timerStarted:Boolean = false;
var counter:uint = 0;
var firstDigitStop:uint = 8;
var secondDigitStop:uint = 16;
var thirdDigitStop:uint = 20;
var filter:BlurFilter = new BlurFilter();
var filterArray:Array = new Array();

// 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.addEventListener(MouseEvent.MOUSE_UP, onRoll);
function onRoll(evt:Event):void
{
if (!timerStarted) {
var filter:BlurFilter = new BlurFilter(60, 120, 1);
var filterArray:Array = new Array();
filterArray.push(filter);

num1_txt.filters = filterArray;
var newNumber1:Number = getRan(10);
num1_txt.text = newNumber1;

num2_txt.filters = filterArray;
var newNumber2:Number = getRan(10);
num2_txt.text = newNumber2;

num3_txt.filters = filterArray;
var newNumber3:Number = getRan(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() {
filter.blurY = 0;
filter.blurX = 0;
num3_txt.filters = new Array(filter);
}

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

addEventListener(Event.ENTER_FRAME, onFrame);
function onFrame(evt:Event):void
{
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.

function getRan(max:uint):uint
{
return Math.floor(Math.random()*max);
}



Everything else is the same, just target at least Flash Player 9 with AS3.



*_*

0 comments:

 

template by blogger templates