Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 21-30 of 39 total

Actionscript _Text Class

Useful functions for adding dynamic text fields. Don't forget, you have to add the font of choice into the main movie Library for this to work (Library Panel Menu > New Font). The name you give the font is the string that you pass to the getTextFormat function for the "font" parameter. Setup your font styles with getTextFormat, setup your dynamic text box with getTextField, and then add text to the text box with appendTextField.

   1  
   2  dynamic class _Text {
   3  	static function getTextFormat(font:String,size:Number,color:Number,leading:Number,url:String,target:String){
   4  		var fmt:TextFormat = new TextFormat();
   5  		fmt.font = font;
   6  		fmt.kerning = true;
   7  		fmt.leading = (leading != undefined && leading != null) ? leading : 0;
   8  		fmt.bold = false;
   9  		fmt.size = (size != undefined && size != null) ? size : 11;
  10  		fmt.color = (color != undefined && color != null) ? color : 0x000000;
  11  
  12  		if (url != undefined && url != null) {
  13  			fmt.url = url;
  14  			fmt.target = (target != undefined && target != null) ? target : "_self";
  15  		}
  16  		return (fmt);
  17  	}
  18  	
  19  	static function getTextField(targetMC,targetDepth,txtFieldName,x,y,w,h,txtObj){
  20  		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
  21  		var txtfld = targetMC.createTextField(txtFieldName, targetDepth, x, y, w, h);
  22  		txtfld.antiAliasType = (txtObj.antiAliasType) ? txtObj.antiAliasType : "advanced";
  23  		txtfld.sharpness = (txtObj.sharpness) ? txtObj.sharpness : -60;
  24  		txtfld.thickness = (txtObj.thickness) ? txtObj.thickness : -100;
  25  		txtfld.embedFonts = (txtObj.embedFonts) ? txtObj.embedFonts : true;
  26  		txtfld.selectable = (txtObj.selectable) ? txtObj.selectable : false;
  27  		txtfld.html = (txtObj.html) ? txtObj.html : true;
  28  		txtfld.multiline = (txtObj.multiline) ? txtObj.multiline : true;
  29  		txtfld.autoSize = (txtObj.autoSize) ? txtObj.autoSize : "left";
  30  		
  31  		if (txtObj.htmlText) txtfld.htmlText = txtObj.htmlText;
  32  		if (txtObj.txtFormat) txtfld.setTextFormat(txtObj.txtFormat);
  33  		
  34  		if (txtObj.wordWrap == undefined || txtObj.wordWrap == null) txtfld._width = txtfld.textWidth + 10;
  35  		else txtfld.wordWrap = txtObj.wordWrap;
  36  		return txtfld;
  37  	}
  38  	
  39  	static function appendTextField(txtfld,txtfrmt,txt){
  40  		var beginIndex:Number = txtfld.htmlText.length;
  41  		txtfld.setNewTextFormat(txtfrmt);
  42  		txtfld.replaceText(beginIndex,beginIndex,txt);
  43  	}
  44  }

Actionscript _Draw Class

These still need tweaking, but essentially I just wanted a repeatable way to draw boxes via script. This includes the ability to add rounded corners, gradients, and drop shadows (could it really be any other way? :) The Balloon function was to fulfill a specific goal, obviously, but since these things are more and more popular I figured it would be a good idea to have it somewhat configurable for future usage. Right now, for drop shadows you just have to make two boxes, one blurry, one not. Perhaps it would be better to consolidate the balloon script into the box script, but for now, this works.

   1  
   2  import flash.filters.BlurFilter;
   3  dynamic class _Draw {
   4  	
   5  	static function box(targetMC,targetDepth,w,h,c,r,gradientType){
   6  		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
   7  		var newBox:MovieClip = targetMC.createEmptyMovieClip("newBox", targetDepth);
   8  		var g:Object = new Object();
   9  		
  10  		if (c.length != undefined){
  11  			g = getGradientObject(w,h,gradientType);
  12  			newBox.beginGradientFill(g.fillType, c, g.alphas, g.ratios, g.matrix, g.spreadMethod, g.interpolationMethod, g.focalPointRatio);
  13  		} else {
  14  			newBox.beginFill(c);
  15  		}
  16  		
  17  		if (r == undefined) var r = 0;
  18  		
  19  		with (newBox){
  20  			lineStyle(0,0x000000,0);
  21  			moveTo(r, 0);
  22  			lineTo(w-r, 0);
  23  			curveTo(w,0,w,r);
  24  			lineTo(w, h-r);
  25  			curveTo(w,h,w-r,h);			
  26  			lineTo(r, h);
  27  			curveTo(0,h,0,h-r);
  28  			lineTo(0, r);
  29  			curveTo(0,0,r,0);
  30  			endFill();
  31  		}
  32  		
  33  		return newBox;
  34  	}
  35  
  36  	static function balloon(targetMC,targetDepth,w,h,c,r,gradientType,arrowBaseOffset,arrowBaseWidth,arrowLength,arrowPointOffset){
  37  		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
  38  		var newBalloon:MovieClip = targetMC.createEmptyMovieClip("newBalloon", targetDepth);
  39  		var g:Object = new Object();
  40  		
  41  		if (c.length != undefined){
  42  			g = getGradientObject(w,h,gradientType);
  43  			newBalloon.beginGradientFill(g.fillType, c, g.alphas, g.ratios, g.matrix, g.spreadMethod, g.interpolationMethod, g.focalPointRatio);
  44  		} else {
  45  			newBalloon.beginFill(c);
  46  		}
  47  		
  48  		if (r == undefined) var r = 0;
  49  		
  50  		with (newBalloon){
  51  			lineStyle(0,0x000000,0);
  52  			moveTo(r, 0);
  53  			lineTo(w-r, 0);
  54  			curveTo(w,0,w,r);
  55  			lineTo(w, h-r);
  56  			curveTo(w,h,w-r,h);
  57  			
  58  			// start arrow			
  59  			lineTo(r + arrowBaseOffset + arrowBaseWidth, h);
  60  			lineTo(r + arrowBaseOffset + (arrowBaseWidth/2) + arrowPointOffset, h + arrowLength);
  61  			lineTo(r + arrowBaseOffset, h);
  62  			// end arrow
  63  			
  64  			lineTo(r, h);
  65  			curveTo(0,h,0,h-r);
  66  			lineTo(0, r);
  67  			curveTo(0,0,r,0);
  68  			endFill();
  69  		}
  70  		
  71  		return newBalloon;
  72  	}
  73  
  74  	static function getGradientObject(w,h,gradientType){
  75  		var gradientObj:Object = new Object();
  76  		
  77  		gradientObj.fillType = "linear"
  78  		gradientObj.alphas = [100, 100];
  79  		gradientObj.ratios = [0, 0xFF];
  80  		gradientObj.spreadMethod = "pad";
  81  		gradientObj.interpolationMethod = "RGB";
  82  		gradientObj.focalPointRatio = 1;
  83  		
  84  		if (gradientType == "vGradient") {
  85  			gradientObj.matrix = {a:0, b:w, c:0, d:-h, e:0, f:0, g:0,h:h/2, i:1};
  86  		} else {
  87  			gradientObj.matrix = {a:w, b:0, c:0, d:0, e:w, f:0, g:w/2, h:0, i:1};
  88  		}
  89  		
  90  		return gradientObj;
  91  	}
  92  	
  93  	static function blur(mc,blurAmount,blurOpacity){
  94  		blurAmount = (blurAmount == undefined) ? 10 : blurAmount;
  95  		blurOpacity = (blurOpacity == undefined) ? 100 : blurOpacity;
  96  		var filter:BlurFilter = new BlurFilter(blurAmount, blurAmount, 2);
  97  		var filterArray:Array = new Array();
  98  		filterArray.push(filter);
  99  		mc.filters = filterArray;
 100  		mc._alpha = blurOpacity
 101  	}
 102  }

Actionscript _Animate Class

This is basically a way to set up an animation queue to utilize scripted tweening. You would have to create external functions which carry forth the animation actions then store the function objects in the _Animate.queue Array in the sequence you wish to play them out. Ideally, these functions would return a Tween object for the sake of utilizing "onMotionStopped". Returning False (bool) will simply allow the next function in the queue to fire without checking for the onMotionStopped call. When you are ready to start the animation sequence, just call launch();

   1  
   2  import _String;
   3  
   4  dynamic class _Animate {
   5  	var queue:Array;
   6  	
   7  	function _Animate() {
   8  		queue = null;
   9  	}
  10  	
  11  	public function caller(qNum){
  12  		var gA = this;
  13  		var callback = queue[qNum].func.apply(this, _String.toArray(queue[qNum].params)); qNum++;
  14  		if (qNum < queue.length && callback) callback.onMotionStopped = function() { gA.caller(qNum); }
  15  		else if (qNum < queue.length && !callback) gA.caller(qNum);
  16  	}
  17  	
  18  	public function launch(){ caller(0); }
  19  	
  20  	
  21  }


Sample Usage

   1  
   2  var zoomIn = new _Animate();
   3  
   4  zoomIn.queue = [
   5  	{func:aniSidebar, params:"hide,6"},
   6  	{func:aniMapMidFade, params:"hide,6"}
   7  ];
   8  
   9  zoomIn.launch();
  10  
  11  function aniSidebar(action, time){
  12  	var leftBeginX = (action == "hide") ? 0 : 0 - sidebarLeftBg._width;
  13  	var leftEndX = (action == "hide") ? 0 - sidebarLeftBg._width : 0;
  14  	var rightBeginX = (action == "hide") ? Stage.width - sidebarRightBg._width : Stage.width;
  15  	var rightEndX = (action == "hide") ? Stage.width : Stage.width - sidebarRightBg._width;
  16  	var transition = mx.transitions.easing.Regular.easeInOut;
  17  	var leftAni = new mx.transitions.Tween(sidebarLeftBg, "_x", transition, leftBeginX, leftEndX, time);
  18  	var rightAni = new mx.transitions.Tween(sidebarRightBg, "_x", transition, rightBeginX, rightEndX, time);
  19  	return rightAni;
  20  }
  21  
  22  function aniMapMidFade(action, time){
  23  	var begin = (action == "hide") ? 100 : 0;
  24  	var end = (action == "hide") ? 0 : 100;
  25  	var transition = mx.transitions.easing.Regular.easeOut;
  26  	var mapMidAni = new mx.transitions.Tween(mapMid, "_alpha", transition, begin, end, time);
  27  	return mapMidAni;
  28  }
  29  

Actionscript _XML Class

   1  
   2  import _String;
   3  
   4  dynamic class _XML extends XML {
   5  	function _XML() {
   6  		
   7  	}
   8  	
   9  	public function $N(tag,xmlNode,xmlNodeArray){
  10  		if (xmlNode == undefined) var xmlNode = this;
  11  		if (xmlNodeArray == undefined) var xmlNodeArray:Array = new Array();
  12  		var nodeArray:Array = new Array();
  13  		
  14  		for (var x=0; x<xmlNode.childNodes.length; x++) {
  15  			if (xmlNode.childNodes[x].nodeType == 1){
  16  				if (xmlNode.childNodes[x].nodeName == tag) xmlNodeArray.push(xmlNode.childNodes[x]);
  17  				$N(tag,xmlNode.childNodes[x],xmlNodeArray);
  18  			}
  19  		}
  20  		return xmlNodeArray;
  21  	}
  22  	
  23  	// If multiple, returns a nodeValue Array
  24  	// If single, returns a nodeValue String
  25  	public function $V(tag,xmlNode) {
  26  		if (xmlNode == undefined) var xmlNode = this;
  27  		var n = $N(tag,xmlNode);
  28  		if (n.length == 1){
  29  			var nV = n[0].firstChild.nodeValue;
  30  			if (nV != undefined){
  31  				nV = escape(nV);
  32  				nV = _String.Replace(nV,"%C2%93","%22");
  33  				nV = _String.Replace(nV,"%C2%94","%22");
  34  				nV = unescape(nV);
  35  			}
  36  			return nV;
  37  		}
  38  		else {
  39  			var vArray:Array = new Array();
  40  			for (var i:String in n) {
  41  				vArray[i] = n[i].firstChild.nodeValue;
  42  				if (vArray[i] != undefined){
  43  					vArray[i] = escape(vArray[i]);
  44  					vArray[i] = _String.Replace(vArray[i],"%C2%93","%22");
  45  					vArray[i] = _String.Replace(vArray[i],"%C2%94","%22");
  46  					vArray[i] = unescape(vArray[i]);
  47  				}
  48  			}
  49  			return vArray;
  50  		}
  51  	}
  52  }

Actionscript _String Class

   1  dynamic class _String {
   2  	// Replace a string or substrings within a string
   3  	static function Replace (the_String, search_String, replace_String, occurrences, backward) {
   4  		if (search_String == replace_String) return the_String;
   5  		var found = 0;
   6  		if (backward == true) {
   7  			var pos = the_String.lastIndexOf(search_String);
   8  			while (pos>= 0) {
   9  				found++;
  10  				var start_String = the_String.substr(0, pos);
  11  				var end_String = the_String.substr(pos + search_String.length);
  12  				the_String = start_String + replace_String + end_String;
  13  				pos = the_String.lastIndexOf(search_String, start_String.length);
  14  				if (found == occurrences) pos = -1;
  15  			}
  16  		}
  17  		else {
  18  			var pos = the_String.indexOf(search_String);
  19  			while (pos>= 0) {
  20  				found++;
  21  				var start_String = the_String.substr(0, pos);
  22  				var end_String = the_String.substr(pos + search_String.length);
  23  				the_String = start_String + replace_String + end_String;
  24  				pos = the_String.indexOf(search_String, pos + replace_String.length);
  25  				if (found == occurrences) pos = -1;
  26  			}
  27  		}
  28  		
  29  		return the_String;
  30  	}
  31  	
  32  	// Convert delimited (comma by default) String to an Array
  33  	static function toArray(string, separator:String) {
  34  		var list = new Array();
  35  		if (typeof(string) == "string"){
  36  			if (separator == undefined) separator = ",";
  37  			if (string == null) return false;
  38  			var currentStringPosition = 0;
  39  			while (currentStringPosition<string.length) {
  40  				var nextIndex = string.indexOf(separator, currentStringPosition);
  41  				if (nextIndex == -1) break;
  42  				var word = string.slice(currentStringPosition, nextIndex);
  43  				list.push(word);
  44  				currentStringPosition = nextIndex+1;
  45  			}
  46  			if (list.length<1) list.push(string);
  47  			else list.push(string.slice(currentStringPosition, string.length));
  48  		} else {
  49  			list.push(string);
  50  		}
  51  		return list;
  52  	}
  53  }

Actionscript Draw Box

Function to draw a box with a specified width, height, and color, and return the box MovieClip

   1  
   2  function draw_box(w,h,c){
   3  	var new_box = this.createEmptyMovieClip("new_box", this.getNextDepth());
   4  	new_box.beginFill(c);
   5  	new_box.lineStyle(0,0x000000,0);
   6  	new_box.moveTo(0, 0);
   7  	new_box.lineTo(w, 0);
   8  	new_box.lineTo(w, h);
   9  	new_box.lineTo(0, h);
  10  	new_box.lineTo(0, 0);
  11  	new_box.endFill();
  12  	return new_box;
  13  }
  14  


Sample function call draws a 200w x 25h pixel, black box:
   1  
   2  draw_box(200,25,0x000000);

Flash 8 AS2 REMOTING with AMFPHP

This code should be used to perform remoting with Flash 8 using ActionScript 2.

   1  
   2  // remoting
   3  import mx.remoting.Service;
   4  import mx.remoting.PendingCall;
   5  import mx.rpc.RelayResponder;
   6  import mx.remoting.debug.NetDebug;
   7  
   8  NetDebug.initialize();
   9  
  10  var myResponder:RelayResponder = new RelayResponder(this,"say_Result","say_Fault");
  11  var siteCFC:Service = new Service("http://127.0.0.1/flashservices/gateway.php",null,"HelloWorld",null,myResponder);
  12  var temp_pc:PendingCall = siteCFC.say("Hola chicos!");
  13  
  14  function say_Result(re){
  15  	trace(re.result);
  16  }
  17  function say_Fault(result){
  18  	trace("error!");
  19  }

PHP: Connecting Flash to a Database (remoting)

There are many ways to do this.

Depending on the backend language you're using, whether PHP, ColdFusion or other... you'll need to create some components (CFC's for ColdFusion), (Classes for PHP)... these are referred to as "services"

Then you'll need to connect Flash to a remoting gateway (I use AMFPHP for PHP: www.amfphp.org).

Either way, you'll need NetServices.as from Flash MX Remoting, call a gateway and retrieving a list of functions from your classes/components/services.

Enough with the theory, here's how to do it with PHP:
   1  
   2  /* /flashservices/services/Catalog.php */
   3  class Catalog {
   4          var $products_array = array();
   5  
   6  // Constructor: Contains the list of methods available to the gateway
   7  function Catalog() {
   8  	$this->methodTable = array (
   9  		"getProducts" => array (
  10  			"description" => "Get list of products",
  11  			"access" => "remote",
  12  			"arguments" => "" // arguments could be optional, not tested
  13  		)
  14  	); // end methodTable
  15  }
  16  
  17  function getProducts() {	
  18  	// your code goes here
  19  
  20  	return $this->products_array;
  21  }
  22  }


!!!!!!!!! The code below is now deprecated !!!!!!!!
---- see my other snippet to do this in Flash 8 ----

Flash ActionScript (PHP Gateway):
   1  
   2  #include "NetServices.as"
   3  NetServices.setDefaultGatewayUrl("http://yourserver.com/flashservices/gateway.php");
   4  gw = NetServices.createGatewayConnection();
   5  CatalogREMOTE = gw.getService("Catalog", this);
   6  CatalogREMOTE.getProducts();
   7  
   8  getProducts_Result = function(result) {
   9  	_root.products_results = result;
  10  }


You parse the _root.products_results array however you want :D

How flash connects with database

// description of your code here

   1  
   2  // insert code here..

Helper to Display Rails Flash Messages

A simple code snippet for displaying your flash[:warning] = "Warning Message" messages in rails.

   1  
   2  def flash_helper
   3    
   4      f_names = [:notice, :warning, :message]
   5      fl = ''
   6      
   7      for name in f_names
   8        if flash[name]
   9          fl = fl + "<div class=\"notice\" id=\"#{name}\">#{flash[name]}</div>"
  10        end
  11        flash[name] = nil;
  12      end
  13      return fl
  14    end
« Newer Snippets
Older Snippets »
Showing 21-30 of 39 total