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 AS3 Helps: Catching XML Load Errors, Asynchronously

Not all errors in ActionScript 3 happen when you call a function. Some errors can occur sometime after the call. A common example of this is loading content from the web like XML or other content. Here is a sample of how to catch these types of errors

var myLoader:URLLoader = new URLLoader();

myLoader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
function catchIOError(event:IOErrorEvent){
trace("Error caught: "+event.type);
}

myLoader.load(new URLRequest("Invalid XML URL"));
trace("Error, continuing...");

This will output:
Error, continuing...
Error caught: ioError

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: Tile Component

When Flash CS3 came out one of the new components was the Tile Component. The Tile Component in AS3 is similar to the Datagrid component in that it uses columns and rows to render data and images. The Tile Component can come in very useful in many instances of design and implementation.

Flash has a few examples of using the Tile Component in the helps. I’ve taken one of those examples and tweaked it a bit below. I put a text field on the stage with an instance name of myText and created a movieClip on the stage as well with the instance name of myMC.

The actionscript for using the Tile Component is as follows.  You’ll notice that it has some of the same properties as the DataGrid…like I mentioned.

import fl.controls.ScrollPolicy;
import fl.controls.TileList;
import fl.data.DataProvider;
import fl.controls.UIScrollBar;
import fl.events.ScrollEvent;
import fl.controls.ScrollBarDirection;

myText.text = "Select an Image";

//add a bunch of images and labels
var dp:DataProvider = new DataProvider();
dp.addItem({label:"Image 1", source:"http://www.helpexamples.com/flash/images/image1.jpg"});
dp.addItem({label:"Image 2", source:"http://www.helpexamples.com/flash/images/image2.jpg"});
dp.addItem({label:"Image 3", source:"http://www.helpexamples.com/flash/images/image3.jpg"});
dp.addItem({label:"Image 4", source:"http://www.helpexamples.com/flash/images/image1.jpg"});
dp.addItem({label:"Image 5", source:"http://www.helpexamples.com/flash/images/image2.jpg"});
dp.addItem({label:"Image 6", source:"http://www.helpexamples.com/flash/images/image3.jpg"});
dp.addItem({label:"Image 7", source:"http://www.helpexamples.com/flash/images/image1.jpg"});
dp.addItem({label:"Image 8", source:"http://www.helpexamples.com/flash/images/image2.jpg"});
dp.addItem({label:"Image 9", source:"http://www.helpexamples.com/flash/images/image3.jpg"});
dp.addItem({label:"Image 10", source:"http://www.helpexamples.com/flash/images/image2.jpg"});
dp.addItem({label:"Image 11", source:"http://www.helpexamples.com/flash/images/image2.jpg"});
dp.addItem({label:"Image 12", source:"http://www.helpexamples.com/flash/images/image3.jpg"});

var myTileList:TileList = new TileList();
myTileList.dataProvider = dp;
myTileList.scrollPolicy = ScrollPolicy.ON;
myTileList.columnWidth = 100;
myTileList.rowHeight = 67;
myTileList.setSize(400,67);
myTileList.columnCount = 3;
myTileList.rowCount = 1;
myTileList.move(10, 300);
addChild(myTileList);

myTileList.addEventListener(MouseEvent.CLICK,itemClicked);
function itemClicked(e:Event):void {
//myText
myText.text = myTileList.selectedItem.label;
//myMC
var i =new Loader();
i.load(new URLRequest(myTileList.selectedItem.source));
myMC.addChild(i);
}

Here are the sample files:

tileComponent.swf
tileComponent.fla

Related Posts:

Flash Papervision 3D Sample > Rotating Image Block

Here’s a Flash AS3 (actionscript 3) Papervision 3D sample.  This sample loads data from an XML file that holds the location of the images.  It then puts them on a plane using the Papervision 3D classes and rotates them.

You need the Flash Player to view this video.

File:
PapervisionImageBlock.fla

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 9 Security Update Verification

Jesse Harding posted this on his blog yesterday on how to verify if a user has the Flash 9 security update…really solid.  So solid I thought I blog it.  Jesse’s article here.

/*

I mashed up some existing frameworks out there to create an alert that displays on your web site if your visitors don’t have the latest version of the Flash player (9.0.124.0). It is highly recommended that all web users install the latest version of the player due to exploits possible in the previous version.

You can see an example of my alert here.
(for testing purposes I requiring Flash 11 here so everyone should get the alert for the time being)

Download the files for my script here.

The zip file download has a readme.txt file that explains how to use the script in detail. I’ll give a brief summary here:

My hack uses swfobject and a  jQuery Thickbox together to display a very noticeable alert.

Normally swfobject is great for verifying the most recent version, but considering the urgent nature of this update, I combined it with a  Thickbox as the alert to make sure it is very visible.

The way I did this was somewhat sneaky. Rather than point to the expressInstall.swf file that swfobject usually defaults to when flash is out of date, I pointed to my own thickBoxWarning.swf which uses ExternalInterface to launch the Thickbox.

If for whatever reason you want to go without the Thickbox simply remove the reference to thickBoxWarning.swf in the swfobject code and you’ll get a standard swfobject warning banner at the top of your site.

Here is an example of what that looks like.

Basically all you need to do to use this is place the flashSecurity folder on your server. The paths in these examples assume your flashSecurity folder is in the same folder as your web pages. If this is not the case, you’ll need to update the paths in the code appropriately. You may also need to change the path to the Thickbox window which is set in the included thickBoxWarning.fla file. Add a couple divs to the top of your page (there are examples in the zip) and you are good to go.

This is a very simple but effective way to protect your users and ensure they are aware of the update.

If you have any questions on this email me at jesse AT frogblade.com

*/

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: