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

About this user

Bryan Gidge http://www.gidge.com

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

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.

dynamic class _Text {
	static function getTextFormat(font:String,size:Number,color:Number,leading:Number,url:String,target:String){
		var fmt:TextFormat = new TextFormat();
		fmt.font = font;
		fmt.kerning = true;
		fmt.leading = (leading != undefined && leading != null) ? leading : 0;
		fmt.bold = false;
		fmt.size = (size != undefined && size != null) ? size : 11;
		fmt.color = (color != undefined && color != null) ? color : 0x000000;

		if (url != undefined && url != null) {
			fmt.url = url;
			fmt.target = (target != undefined && target != null) ? target : "_self";
		}
		return (fmt);
	}
	
	static function getTextField(targetMC,targetDepth,txtFieldName,x,y,w,h,txtObj){
		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
		var txtfld = targetMC.createTextField(txtFieldName, targetDepth, x, y, w, h);
		txtfld.antiAliasType = (txtObj.antiAliasType) ? txtObj.antiAliasType : "advanced";
		txtfld.sharpness = (txtObj.sharpness) ? txtObj.sharpness : -60;
		txtfld.thickness = (txtObj.thickness) ? txtObj.thickness : -100;
		txtfld.embedFonts = (txtObj.embedFonts) ? txtObj.embedFonts : true;
		txtfld.selectable = (txtObj.selectable) ? txtObj.selectable : false;
		txtfld.html = (txtObj.html) ? txtObj.html : true;
		txtfld.multiline = (txtObj.multiline) ? txtObj.multiline : true;
		txtfld.autoSize = (txtObj.autoSize) ? txtObj.autoSize : "left";
		
		if (txtObj.htmlText) txtfld.htmlText = txtObj.htmlText;
		if (txtObj.txtFormat) txtfld.setTextFormat(txtObj.txtFormat);
		
		if (txtObj.wordWrap == undefined || txtObj.wordWrap == null) txtfld._width = txtfld.textWidth + 10;
		else txtfld.wordWrap = txtObj.wordWrap;
		return txtfld;
	}
	
	static function appendTextField(txtfld,txtfrmt,txt){
		var beginIndex:Number = txtfld.htmlText.length;
		txtfld.setNewTextFormat(txtfrmt);
		txtfld.replaceText(beginIndex,beginIndex,txt);
	}
}

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.

import flash.filters.BlurFilter;
dynamic class _Draw {
	
	static function box(targetMC,targetDepth,w,h,c,r,gradientType){
		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
		var newBox:MovieClip = targetMC.createEmptyMovieClip("newBox", targetDepth);
		var g:Object = new Object();
		
		if (c.length != undefined){
			g = getGradientObject(w,h,gradientType);
			newBox.beginGradientFill(g.fillType, c, g.alphas, g.ratios, g.matrix, g.spreadMethod, g.interpolationMethod, g.focalPointRatio);
		} else {
			newBox.beginFill(c);
		}
		
		if (r == undefined) var r = 0;
		
		with (newBox){
			lineStyle(0,0x000000,0);
			moveTo(r, 0);
			lineTo(w-r, 0);
			curveTo(w,0,w,r);
			lineTo(w, h-r);
			curveTo(w,h,w-r,h);			
			lineTo(r, h);
			curveTo(0,h,0,h-r);
			lineTo(0, r);
			curveTo(0,0,r,0);
			endFill();
		}
		
		return newBox;
	}

	static function balloon(targetMC,targetDepth,w,h,c,r,gradientType,arrowBaseOffset,arrowBaseWidth,arrowLength,arrowPointOffset){
		var targetDepth = (targetDepth != undefined && targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();
		var newBalloon:MovieClip = targetMC.createEmptyMovieClip("newBalloon", targetDepth);
		var g:Object = new Object();
		
		if (c.length != undefined){
			g = getGradientObject(w,h,gradientType);
			newBalloon.beginGradientFill(g.fillType, c, g.alphas, g.ratios, g.matrix, g.spreadMethod, g.interpolationMethod, g.focalPointRatio);
		} else {
			newBalloon.beginFill(c);
		}
		
		if (r == undefined) var r = 0;
		
		with (newBalloon){
			lineStyle(0,0x000000,0);
			moveTo(r, 0);
			lineTo(w-r, 0);
			curveTo(w,0,w,r);
			lineTo(w, h-r);
			curveTo(w,h,w-r,h);
			
			// start arrow			
			lineTo(r + arrowBaseOffset + arrowBaseWidth, h);
			lineTo(r + arrowBaseOffset + (arrowBaseWidth/2) + arrowPointOffset, h + arrowLength);
			lineTo(r + arrowBaseOffset, h);
			// end arrow
			
			lineTo(r, h);
			curveTo(0,h,0,h-r);
			lineTo(0, r);
			curveTo(0,0,r,0);
			endFill();
		}
		
		return newBalloon;
	}

	static function getGradientObject(w,h,gradientType){
		var gradientObj:Object = new Object();
		
		gradientObj.fillType = "linear"
		gradientObj.alphas = [100, 100];
		gradientObj.ratios = [0, 0xFF];
		gradientObj.spreadMethod = "pad";
		gradientObj.interpolationMethod = "RGB";
		gradientObj.focalPointRatio = 1;
		
		if (gradientType == "vGradient") {
			gradientObj.matrix = {a:0, b:w, c:0, d:-h, e:0, f:0, g:0,h:h/2, i:1};
		} else {
			gradientObj.matrix = {a:w, b:0, c:0, d:0, e:w, f:0, g:w/2, h:0, i:1};
		}
		
		return gradientObj;
	}
	
	static function blur(mc,blurAmount,blurOpacity){
		blurAmount = (blurAmount == undefined) ? 10 : blurAmount;
		blurOpacity = (blurOpacity == undefined) ? 100 : blurOpacity;
		var filter:BlurFilter = new BlurFilter(blurAmount, blurAmount, 2);
		var filterArray:Array = new Array();
		filterArray.push(filter);
		mc.filters = filterArray;
		mc._alpha = blurOpacity
	}
}

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();

import _String;

dynamic class _Animate {
	var queue:Array;
	
	function _Animate() {
		queue = null;
	}
	
	public function caller(qNum){
		var gA = this;
		var callback = queue[qNum].func.apply(this, _String.toArray(queue[qNum].params)); qNum++;
		if (qNum < queue.length && callback) callback.onMotionStopped = function() { gA.caller(qNum); }
		else if (qNum < queue.length && !callback) gA.caller(qNum);
	}
	
	public function launch(){ caller(0); }
	
	
}


Sample Usage

var zoomIn = new _Animate();

zoomIn.queue = [
	{func:aniSidebar, params:"hide,6"},
	{func:aniMapMidFade, params:"hide,6"}
];

zoomIn.launch();

function aniSidebar(action, time){
	var leftBeginX = (action == "hide") ? 0 : 0 - sidebarLeftBg._width;
	var leftEndX = (action == "hide") ? 0 - sidebarLeftBg._width : 0;
	var rightBeginX = (action == "hide") ? Stage.width - sidebarRightBg._width : Stage.width;
	var rightEndX = (action == "hide") ? Stage.width : Stage.width - sidebarRightBg._width;
	var transition = mx.transitions.easing.Regular.easeInOut;
	var leftAni = new mx.transitions.Tween(sidebarLeftBg, "_x", transition, leftBeginX, leftEndX, time);
	var rightAni = new mx.transitions.Tween(sidebarRightBg, "_x", transition, rightBeginX, rightEndX, time);
	return rightAni;
}

function aniMapMidFade(action, time){
	var begin = (action == "hide") ? 100 : 0;
	var end = (action == "hide") ? 0 : 100;
	var transition = mx.transitions.easing.Regular.easeOut;
	var mapMidAni = new mx.transitions.Tween(mapMid, "_alpha", transition, begin, end, time);
	return mapMidAni;
}

Actionscript _XML Class

import _String;

dynamic class _XML extends XML {
	function _XML() {
		
	}
	
	public function $N(tag,xmlNode,xmlNodeArray){
		if (xmlNode == undefined) var xmlNode = this;
		if (xmlNodeArray == undefined) var xmlNodeArray:Array = new Array();
		var nodeArray:Array = new Array();
		
		for (var x=0; x<xmlNode.childNodes.length; x++) {
			if (xmlNode.childNodes[x].nodeType == 1){
				if (xmlNode.childNodes[x].nodeName == tag) xmlNodeArray.push(xmlNode.childNodes[x]);
				$N(tag,xmlNode.childNodes[x],xmlNodeArray);
			}
		}
		return xmlNodeArray;
	}
	
	// If multiple, returns a nodeValue Array
	// If single, returns a nodeValue String
	public function $V(tag,xmlNode) {
		if (xmlNode == undefined) var xmlNode = this;
		var n = $N(tag,xmlNode);
		if (n.length == 1){
			var nV = n[0].firstChild.nodeValue;
			if (nV != undefined){
				nV = escape(nV);
				nV = _String.Replace(nV,"%C2%93","%22");
				nV = _String.Replace(nV,"%C2%94","%22");
				nV = unescape(nV);
			}
			return nV;
		}
		else {
			var vArray:Array = new Array();
			for (var i:String in n) {
				vArray[i] = n[i].firstChild.nodeValue;
				if (vArray[i] != undefined){
					vArray[i] = escape(vArray[i]);
					vArray[i] = _String.Replace(vArray[i],"%C2%93","%22");
					vArray[i] = _String.Replace(vArray[i],"%C2%94","%22");
					vArray[i] = unescape(vArray[i]);
				}
			}
			return vArray;
		}
	}
}

Actionscript _String Class

dynamic class _String {
	// Replace a string or substrings within a string
	static function Replace (the_String, search_String, replace_String, occurrences, backward) {
		if (search_String == replace_String) return the_String;
		var found = 0;
		if (backward == true) {
			var pos = the_String.lastIndexOf(search_String);
			while (pos>= 0) {
				found++;
				var start_String = the_String.substr(0, pos);
				var end_String = the_String.substr(pos + search_String.length);
				the_String = start_String + replace_String + end_String;
				pos = the_String.lastIndexOf(search_String, start_String.length);
				if (found == occurrences) pos = -1;
			}
		}
		else {
			var pos = the_String.indexOf(search_String);
			while (pos>= 0) {
				found++;
				var start_String = the_String.substr(0, pos);
				var end_String = the_String.substr(pos + search_String.length);
				the_String = start_String + replace_String + end_String;
				pos = the_String.indexOf(search_String, pos + replace_String.length);
				if (found == occurrences) pos = -1;
			}
		}
		
		return the_String;
	}
	
	// Convert delimited (comma by default) String to an Array
	static function toArray(string, separator:String) {
		var list = new Array();
		if (typeof(string) == "string"){
			if (separator == undefined) separator = ",";
			if (string == null) return false;
			var currentStringPosition = 0;
			while (currentStringPosition<string.length) {
				var nextIndex = string.indexOf(separator, currentStringPosition);
				if (nextIndex == -1) break;
				var word = string.slice(currentStringPosition, nextIndex);
				list.push(word);
				currentStringPosition = nextIndex+1;
			}
			if (list.length<1) list.push(string);
			else list.push(string.slice(currentStringPosition, string.length));
		} else {
			list.push(string);
		}
		return list;
	}
}

Actionscript Draw Box

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

function draw_box(w,h,c){
	var new_box = this.createEmptyMovieClip("new_box", this.getNextDepth());
	new_box.beginFill(c);
	new_box.lineStyle(0,0x000000,0);
	new_box.moveTo(0, 0);
	new_box.lineTo(w, 0);
	new_box.lineTo(w, h);
	new_box.lineTo(0, h);
	new_box.lineTo(0, 0);
	new_box.endFill();
	return new_box;
}



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

Javascript Window OnLoad Listener

This script gives the ability to run javascript functions after the page has loaded. I definitely can't take credit for this, as it's one I found, but I use it constantly.

window.onloadListeners=new Array();

window.addOnLoadListener = function (listener) {
	window.onloadListeners[window.onloadListeners.length]=listener;
}

window.onload=function(){
	for(var i=0;i<window.onloadListeners.length;i++){
		func = window.onloadListeners[i];
		func.call();
	}
}

//window.addOnLoadListener(function);
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS