Archive for the 'Snippet' Category

16
Apr

AS3 Email Validation Code Snippets - Regular Expression

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Regular expressions are something that were introduced in AS3 with a sigh of relief from all flash developers. Yay

Below is a code snippet posted by senocular at kriupa

function isValidEmail(email:String):Boolean {    var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;

return emailExpression.test(email);

}

//...

trace(isValidEmail("senocular@example.com")); // true

trace(isValidEmail("@example.com")); // false

trace(isValidEmail("senocular@example")); // false

trace(isValidEmail("seno\\cular@example.com")); // false

Its a beautiful thing.

Found this useful? How about buying me a coffee - simply click here.

11
Apr

Simple Find and Replace Function

This is a simple but effective function allows you to search a string and replace characters

function searchAndReplace(holder:String, searchfor:String, replacement:String) {
		var temparray:Array = new Array();
		temparray = holder.split(searchfor);
		holder = temparray.join(replacement);
		return (holder);
	}

Found this useful? How about buying me a coffee - simply click here.

14
Feb

Getting List of Child MovieClips in AS3

Here is how you find all the movieclips inside an object in as3

var desc:XML = describeType(my_mc);
for each (var node:XML in desc.variable) {
    if (node.@type == "flash.display::MovieClip") {
        if (my_mc.getChildByName(node.@name.toString())) {
            trace("child mc found");
        }
    }
}

Found this useful? How about buying me a coffee - simply click here.

13
Feb

load external files / assets with AS3

This example shows you how to load and external asset (file) in AS3. The files can be a swf, jpg, gif or png.

This particular example loads a jpg called loademe.jpg and attaches to the stage

Load Asset / external files example
// loadAsset.as
package {
	import flash.display.*;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.events.*;

	public class loadAsset extends Sprite{

		private var _loadPath:String
		private var _loader:Loader;

		// Constructor
		public function loadAsset(){
			// Create the loader
			_loader = new Loader();

			// Add event listener that tells us when the
			// asset can be safely accessed
			_loader.contentLoaderInfo.addEventListener(Event.INIT, displayAsset);

			// Create new URLRequest
			var urlRequest = new URLRequest("loadme.jpg")

			// Start the asset load
			_loader.load(urlRequest);
		}

		// Display the asset once the load is complete
		private function displayAsset(ev:Event){
			// it to the DisplayObjectContainer
			addChild(_loader.content);
		}
	}
}

To load the asset/file you need to open the flash file and place this AS in the timeline

import loadAsset;
addChild(new loadAsset());

Found this useful? How about buying me a coffee - simply click here.

12
Feb

Mouse Event - click

In AS3 mouse event a handled differently than in AS2. Here is the general procedure for adding a click event to a object.

Full code after the jump.

Mouse Event - click
// you will need a movieclip on the stage with an instance name of cir_mc
// or just download the file above (www.flashcs.org)

cir_mc.addEventListener(MouseEvent.CLICK, clickListner);

function clickListner(ev:MouseEvent):void{
	trace("Circle Clicked");
}

Found this useful? How about buying me a coffee - simply click here.

10
Feb

Mouse Event - over

In AS3 mouse event a handled differently than in AS2. Here is the general procedure for adding a mouse over event to a object.

Mouse Event - over
// you will need a movieclip on the stage with an instance name of cir_mc
// or just download the file above (www.flashcs.org)

cir_mc.addEventListener(MouseEvent.MOUSE_OVER, clickListner);

function clickListner(ev:MouseEvent):void{
	trace("Mouse Over Circle");
}

Found this useful? How about buying me a coffee - simply click here.

06
Feb

Drag in AS3

You can place this code in an AS3 document or a custom class, your choice. Thanks to Matt for the snippet Full code after the jump.

AS3 click and drag example
function initDragger(mc:MovieClip):void

{

 mc.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void

 {

 	e.currentTarget.startDrag();

 });

 mc.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void

 {

 	e.currentTarget.stopDrag();

 });}

// Set up drag
initDragger(cir_mc);

Found this useful? How about buying me a coffee - simply click here.

04
Feb

Email Validation in AS2

I Have just posted up a code snippet that validates general email addresses in AS2. [Update] - Click here for the AS3 Email Validation Code

Full code after the jump.

function validateEmail(emailStr:String) {
var errorCount:Number = 0;
//check to see if there are at least two chars be for the @ symbol
if (emailStr.indexOf("@")<2) {
errorCount++;
}
//check that the @ is not within 2 chars of .
if (emailStr.lastIndexOf(".")<=(emailStr.lastIndexOf("@")+2)) {
errorCount++;
}
//check length eg min aa@bb.cc
if (emailStr.length<8) {
errorCount++;
}
//check there is only one @
if (emailStr.indexOf("@") != emailStr.lastIndexOf("@")) {
errorCount++;
}
// return a T/F to the flash file
if (errorCount == 0) {
return true;
} else {
return false;
}
}

//to use it
if(validateEmail(email.text)){
trace("EMAIL VALID");
}else{
trace("EMAIL NOT VALID")
}

Found this useful? How about buying me a coffee - simply click here.




Close
E-mail It