Flash AS3: Random Number Function, Returns a Random Number in Range

Here’s a little Flash AS3 (ActionScript 3) function that returns a random number in a range given two numbers. Comes in handy when doing many things that require random values within a set range.

function randomRange(minNum:Number, maxNum:Number):Number {
var myRandomNum:Number = Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum;
return myRandomNum;
}

//usuage
randomRange(2,22);

Related Posts:

Flash AS3 (actionscript 3) Helps: Flip MovieClip Horizontally with Matrix Class

The Flash Actionscript 3 Matrix class represents a transformation matrix that determines how to map points from one coordinate space to another. You can perform various graphical transformations on a display object by setting the properties of a Matrix object, applying that Matrix object to the matrix property of a Transform object, and then applying that Transform object as the transform property of the display object. These transformation functions include translation (x and y repositioning), rotation, scaling, and skewing.

Here’s a sample that uses the Matrix class to flip a Movie Clip horizontally:

function flipHorizontal(dsp:DisplayObject):void {
var matrix:Matrix=dsp.transform.matrix;
matrix.transformPoint(new Point(dsp.width/2,dsp.height/2));
if (matrix.a>0) {
matrix.a=-1*matrix.a;
matrix.tx=dsp.width+dsp.x;
} else {
matrix.a=-1*matrix.a;
matrix.tx=dsp.x-dsp.width;
}
dsp.transform.matrix=matrix;
}

//usuage
flipHorizontal(myMC)

Related Posts:

Flash Helps: Create Text Field or Text Fields Dynamically

Here’s a simple little example of creating actionscript 3 text fields dynamically.  It is also a great example of using functions to do all the dirty work for you.

In order for this to work you need to put a button component in the library.

This sample will create an input text field instance and a button instance.  When the user click on the button, it will create a new text field and set its x,y to random.

Here is the code:

import fl.controls.Button;
import flash.text.TextField;
import flash.text.TextFieldType;

//create a button
var myButton:Button = new Button();
myButton.label = "set text random x,y";
myButton.emphasized = true;
myButton.width = 150;
myButton.move(50, 100);
addChild(myButton);

var tf1:TextField = createTextField(10, 10, 400, 22, true);
tf1.type = TextFieldType.INPUT;

//create an input text field function
function createTextField(x:Number, y:Number, width:Number, height:Number, border:Boolean):TextField {
     var result:TextField = new TextField();
     result.x = x;
     result.y = y;
     result.width = width;
     result.height = height;
     result.background = true;
     result.border = border;
     addChild(result);
     return result;
}

//event that listens for the button to be clicked
myButton.addEventListener(MouseEvent.CLICK,setRandomXY);

//function that creates the new text field and sets the text
function setRandomXY(e:Event){
     var randomX = Math.random()*stage.width;
     var randomY = Math.random()*stage.height;
     var tf2:TextField = createTextField(randomX,randomY,50,20,false)
     tf2.type = TextFieldType.DYNAMIC;
     tf2.text = tf1.text;
}

Related Posts:

Flash Helps: Flashvars in AS3

Another new feature in AS3 ( actionscript 3 ) is the new method of calling and setting flashvars in your actionscript.  Back in actionscript 2, you just call them after setting them in your html code.  AS3 is different.  While there are a few methods to do this, the simplest way I’ve found is this:

//the name of my flashvar is myVar

myText.text = root.loaderInfo.parameters.myVar;

Then you can use the Flash AS3 global variable method I use here to set your flashvar in as3 as a global variable.

MyGlobal.myString = root.loaderInfo.parameters.myVar;

Related Posts:

Flash Helps: XML and AS3 (actionsript 3) Flash

XML AS3 (actionscript 3) Flash sample? So I was all ready to write up a great AS3 XML sample using the new E4X and Flash CS3 and I see that Jesse Harding beat me to it.

With the release of Flash Player 9 a few years ago, a new virtual machine called ActionScript Virtual Machine 2 (AVM2), which increases player performance. Improvements to the ActionScript language in ActionScript 3.0 offer Flash geeks new functionality when creating Flash applications.
As Jesse discovered…with AS3 (and AVM2) there are a lot more XML ways to retrieve data. E4X provides faster and more ideal access to XML data. Use it at runtime to pull data directly from an XML object, which means you don’t need to write a specialized parser. In AS2 the XML data had to be changed from an XML packet into structured data, like arrays, in order for Flash ActionScript to use the data. Now this process is way more intuitive and logical making XML data easier to utilize using ActionScript 3.0.

Check out Jesse’s sample.

Related Posts: