<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: class code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 08:12:23 GMT</pubDate>
    <description>DZone Snippets: class code</description>
    <item>
      <title>Actionscript _Text Class</title>
      <link>http://snippets.dzone.com/posts/show/3517</link>
      <description>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 &gt; 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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;dynamic class _Text {&lt;br /&gt;	static function getTextFormat(font:String,size:Number,color:Number,leading:Number,url:String,target:String){&lt;br /&gt;		var fmt:TextFormat = new TextFormat();&lt;br /&gt;		fmt.font = font;&lt;br /&gt;		fmt.kerning = true;&lt;br /&gt;		fmt.leading = (leading != undefined &amp;&amp; leading != null) ? leading : 0;&lt;br /&gt;		fmt.bold = false;&lt;br /&gt;		fmt.size = (size != undefined &amp;&amp; size != null) ? size : 11;&lt;br /&gt;		fmt.color = (color != undefined &amp;&amp; color != null) ? color : 0x000000;&lt;br /&gt;&lt;br /&gt;		if (url != undefined &amp;&amp; url != null) {&lt;br /&gt;			fmt.url = url;&lt;br /&gt;			fmt.target = (target != undefined &amp;&amp; target != null) ? target : "_self";&lt;br /&gt;		}&lt;br /&gt;		return (fmt);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	static function getTextField(targetMC,targetDepth,txtFieldName,x,y,w,h,txtObj){&lt;br /&gt;		var targetDepth = (targetDepth != undefined &amp;&amp; targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();&lt;br /&gt;		var txtfld = targetMC.createTextField(txtFieldName, targetDepth, x, y, w, h);&lt;br /&gt;		txtfld.antiAliasType = (txtObj.antiAliasType) ? txtObj.antiAliasType : "advanced";&lt;br /&gt;		txtfld.sharpness = (txtObj.sharpness) ? txtObj.sharpness : -60;&lt;br /&gt;		txtfld.thickness = (txtObj.thickness) ? txtObj.thickness : -100;&lt;br /&gt;		txtfld.embedFonts = (txtObj.embedFonts) ? txtObj.embedFonts : true;&lt;br /&gt;		txtfld.selectable = (txtObj.selectable) ? txtObj.selectable : false;&lt;br /&gt;		txtfld.html = (txtObj.html) ? txtObj.html : true;&lt;br /&gt;		txtfld.multiline = (txtObj.multiline) ? txtObj.multiline : true;&lt;br /&gt;		txtfld.autoSize = (txtObj.autoSize) ? txtObj.autoSize : "left";&lt;br /&gt;		&lt;br /&gt;		if (txtObj.htmlText) txtfld.htmlText = txtObj.htmlText;&lt;br /&gt;		if (txtObj.txtFormat) txtfld.setTextFormat(txtObj.txtFormat);&lt;br /&gt;		&lt;br /&gt;		if (txtObj.wordWrap == undefined || txtObj.wordWrap == null) txtfld._width = txtfld.textWidth + 10;&lt;br /&gt;		else txtfld.wordWrap = txtObj.wordWrap;&lt;br /&gt;		return txtfld;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	static function appendTextField(txtfld,txtfrmt,txt){&lt;br /&gt;		var beginIndex:Number = txtfld.htmlText.length;&lt;br /&gt;		txtfld.setNewTextFormat(txtfrmt);&lt;br /&gt;		txtfld.replaceText(beginIndex,beginIndex,txt);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 13 Feb 2007 20:17:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3517</guid>
      <author>bgidge (Bryan Gidge)</author>
    </item>
    <item>
      <title>Actionscript _Draw Class</title>
      <link>http://snippets.dzone.com/posts/show/3515</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import flash.filters.BlurFilter;&lt;br /&gt;dynamic class _Draw {&lt;br /&gt;	&lt;br /&gt;	static function box(targetMC,targetDepth,w,h,c,r,gradientType){&lt;br /&gt;		var targetDepth = (targetDepth != undefined &amp;&amp; targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();&lt;br /&gt;		var newBox:MovieClip = targetMC.createEmptyMovieClip("newBox", targetDepth);&lt;br /&gt;		var g:Object = new Object();&lt;br /&gt;		&lt;br /&gt;		if (c.length != undefined){&lt;br /&gt;			g = getGradientObject(w,h,gradientType);&lt;br /&gt;			newBox.beginGradientFill(g.fillType, c, g.alphas, g.ratios, g.matrix, g.spreadMethod, g.interpolationMethod, g.focalPointRatio);&lt;br /&gt;		} else {&lt;br /&gt;			newBox.beginFill(c);&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		if (r == undefined) var r = 0;&lt;br /&gt;		&lt;br /&gt;		with (newBox){&lt;br /&gt;			lineStyle(0,0x000000,0);&lt;br /&gt;			moveTo(r, 0);&lt;br /&gt;			lineTo(w-r, 0);&lt;br /&gt;			curveTo(w,0,w,r);&lt;br /&gt;			lineTo(w, h-r);&lt;br /&gt;			curveTo(w,h,w-r,h);			&lt;br /&gt;			lineTo(r, h);&lt;br /&gt;			curveTo(0,h,0,h-r);&lt;br /&gt;			lineTo(0, r);&lt;br /&gt;			curveTo(0,0,r,0);&lt;br /&gt;			endFill();&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		return newBox;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	static function balloon(targetMC,targetDepth,w,h,c,r,gradientType,arrowBaseOffset,arrowBaseWidth,arrowLength,arrowPointOffset){&lt;br /&gt;		var targetDepth = (targetDepth != undefined &amp;&amp; targetDepth != null) ? targetDepth : targetMC.getNextHighestDepth();&lt;br /&gt;		var newBalloon:MovieClip = targetMC.createEmptyMovieClip("newBalloon", targetDepth);&lt;br /&gt;		var g:Object = new Object();&lt;br /&gt;		&lt;br /&gt;		if (c.length != undefined){&lt;br /&gt;			g = getGradientObject(w,h,gradientType);&lt;br /&gt;			newBalloon.beginGradientFill(g.fillType, c, g.alphas, g.ratios, g.matrix, g.spreadMethod, g.interpolationMethod, g.focalPointRatio);&lt;br /&gt;		} else {&lt;br /&gt;			newBalloon.beginFill(c);&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		if (r == undefined) var r = 0;&lt;br /&gt;		&lt;br /&gt;		with (newBalloon){&lt;br /&gt;			lineStyle(0,0x000000,0);&lt;br /&gt;			moveTo(r, 0);&lt;br /&gt;			lineTo(w-r, 0);&lt;br /&gt;			curveTo(w,0,w,r);&lt;br /&gt;			lineTo(w, h-r);&lt;br /&gt;			curveTo(w,h,w-r,h);&lt;br /&gt;			&lt;br /&gt;			// start arrow			&lt;br /&gt;			lineTo(r + arrowBaseOffset + arrowBaseWidth, h);&lt;br /&gt;			lineTo(r + arrowBaseOffset + (arrowBaseWidth/2) + arrowPointOffset, h + arrowLength);&lt;br /&gt;			lineTo(r + arrowBaseOffset, h);&lt;br /&gt;			// end arrow&lt;br /&gt;			&lt;br /&gt;			lineTo(r, h);&lt;br /&gt;			curveTo(0,h,0,h-r);&lt;br /&gt;			lineTo(0, r);&lt;br /&gt;			curveTo(0,0,r,0);&lt;br /&gt;			endFill();&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		return newBalloon;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	static function getGradientObject(w,h,gradientType){&lt;br /&gt;		var gradientObj:Object = new Object();&lt;br /&gt;		&lt;br /&gt;		gradientObj.fillType = "linear"&lt;br /&gt;		gradientObj.alphas = [100, 100];&lt;br /&gt;		gradientObj.ratios = [0, 0xFF];&lt;br /&gt;		gradientObj.spreadMethod = "pad";&lt;br /&gt;		gradientObj.interpolationMethod = "RGB";&lt;br /&gt;		gradientObj.focalPointRatio = 1;&lt;br /&gt;		&lt;br /&gt;		if (gradientType == "vGradient") {&lt;br /&gt;			gradientObj.matrix = {a:0, b:w, c:0, d:-h, e:0, f:0, g:0,h:h/2, i:1};&lt;br /&gt;		} else {&lt;br /&gt;			gradientObj.matrix = {a:w, b:0, c:0, d:0, e:w, f:0, g:w/2, h:0, i:1};&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		return gradientObj;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	static function blur(mc,blurAmount,blurOpacity){&lt;br /&gt;		blurAmount = (blurAmount == undefined) ? 10 : blurAmount;&lt;br /&gt;		blurOpacity = (blurOpacity == undefined) ? 100 : blurOpacity;&lt;br /&gt;		var filter:BlurFilter = new BlurFilter(blurAmount, blurAmount, 2);&lt;br /&gt;		var filterArray:Array = new Array();&lt;br /&gt;		filterArray.push(filter);&lt;br /&gt;		mc.filters = filterArray;&lt;br /&gt;		mc._alpha = blurOpacity&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 13 Feb 2007 19:57:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3515</guid>
      <author>bgidge (Bryan Gidge)</author>
    </item>
    <item>
      <title>Actionscript _Animate Class</title>
      <link>http://snippets.dzone.com/posts/show/3514</link>
      <description>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();&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import _String;&lt;br /&gt;&lt;br /&gt;dynamic class _Animate {&lt;br /&gt;	var queue:Array;&lt;br /&gt;	&lt;br /&gt;	function _Animate() {&lt;br /&gt;		queue = null;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	public function caller(qNum){&lt;br /&gt;		var gA = this;&lt;br /&gt;		var callback = queue[qNum].func.apply(this, _String.toArray(queue[qNum].params)); qNum++;&lt;br /&gt;		if (qNum &lt; queue.length &amp;&amp; callback) callback.onMotionStopped = function() { gA.caller(qNum); }&lt;br /&gt;		else if (qNum &lt; queue.length &amp;&amp; !callback) gA.caller(qNum);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	public function launch(){ caller(0); }&lt;br /&gt;	&lt;br /&gt;	&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Sample Usage&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;var zoomIn = new _Animate();&lt;br /&gt;&lt;br /&gt;zoomIn.queue = [&lt;br /&gt;	{func:aniSidebar, params:"hide,6"},&lt;br /&gt;	{func:aniMapMidFade, params:"hide,6"}&lt;br /&gt;];&lt;br /&gt;&lt;br /&gt;zoomIn.launch();&lt;br /&gt;&lt;br /&gt;function aniSidebar(action, time){&lt;br /&gt;	var leftBeginX = (action == "hide") ? 0 : 0 - sidebarLeftBg._width;&lt;br /&gt;	var leftEndX = (action == "hide") ? 0 - sidebarLeftBg._width : 0;&lt;br /&gt;	var rightBeginX = (action == "hide") ? Stage.width - sidebarRightBg._width : Stage.width;&lt;br /&gt;	var rightEndX = (action == "hide") ? Stage.width : Stage.width - sidebarRightBg._width;&lt;br /&gt;	var transition = mx.transitions.easing.Regular.easeInOut;&lt;br /&gt;	var leftAni = new mx.transitions.Tween(sidebarLeftBg, "_x", transition, leftBeginX, leftEndX, time);&lt;br /&gt;	var rightAni = new mx.transitions.Tween(sidebarRightBg, "_x", transition, rightBeginX, rightEndX, time);&lt;br /&gt;	return rightAni;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function aniMapMidFade(action, time){&lt;br /&gt;	var begin = (action == "hide") ? 100 : 0;&lt;br /&gt;	var end = (action == "hide") ? 0 : 100;&lt;br /&gt;	var transition = mx.transitions.easing.Regular.easeOut;&lt;br /&gt;	var mapMidAni = new mx.transitions.Tween(mapMid, "_alpha", transition, begin, end, time);&lt;br /&gt;	return mapMidAni;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 13 Feb 2007 19:47:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3514</guid>
      <author>bgidge (Bryan Gidge)</author>
    </item>
    <item>
      <title>Actionscript _XML Class</title>
      <link>http://snippets.dzone.com/posts/show/3513</link>
      <description>&lt;code&gt;&lt;br /&gt;import _String;&lt;br /&gt;&lt;br /&gt;dynamic class _XML extends XML {&lt;br /&gt;	function _XML() {&lt;br /&gt;		&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	public function $N(tag,xmlNode,xmlNodeArray){&lt;br /&gt;		if (xmlNode == undefined) var xmlNode = this;&lt;br /&gt;		if (xmlNodeArray == undefined) var xmlNodeArray:Array = new Array();&lt;br /&gt;		var nodeArray:Array = new Array();&lt;br /&gt;		&lt;br /&gt;		for (var x=0; x&lt;xmlNode.childNodes.length; x++) {&lt;br /&gt;			if (xmlNode.childNodes[x].nodeType == 1){&lt;br /&gt;				if (xmlNode.childNodes[x].nodeName == tag) xmlNodeArray.push(xmlNode.childNodes[x]);&lt;br /&gt;				$N(tag,xmlNode.childNodes[x],xmlNodeArray);&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		return xmlNodeArray;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	// If multiple, returns a nodeValue Array&lt;br /&gt;	// If single, returns a nodeValue String&lt;br /&gt;	public function $V(tag,xmlNode) {&lt;br /&gt;		if (xmlNode == undefined) var xmlNode = this;&lt;br /&gt;		var n = $N(tag,xmlNode);&lt;br /&gt;		if (n.length == 1){&lt;br /&gt;			var nV = n[0].firstChild.nodeValue;&lt;br /&gt;			if (nV != undefined){&lt;br /&gt;				nV = escape(nV);&lt;br /&gt;				nV = _String.Replace(nV,"%C2%93","%22");&lt;br /&gt;				nV = _String.Replace(nV,"%C2%94","%22");&lt;br /&gt;				nV = unescape(nV);&lt;br /&gt;			}&lt;br /&gt;			return nV;&lt;br /&gt;		}&lt;br /&gt;		else {&lt;br /&gt;			var vArray:Array = new Array();&lt;br /&gt;			for (var i:String in n) {&lt;br /&gt;				vArray[i] = n[i].firstChild.nodeValue;&lt;br /&gt;				if (vArray[i] != undefined){&lt;br /&gt;					vArray[i] = escape(vArray[i]);&lt;br /&gt;					vArray[i] = _String.Replace(vArray[i],"%C2%93","%22");&lt;br /&gt;					vArray[i] = _String.Replace(vArray[i],"%C2%94","%22");&lt;br /&gt;					vArray[i] = unescape(vArray[i]);&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			return vArray;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 13 Feb 2007 19:32:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3513</guid>
      <author>bgidge (Bryan Gidge)</author>
    </item>
    <item>
      <title>Actionscript _String Class</title>
      <link>http://snippets.dzone.com/posts/show/3512</link>
      <description>&lt;code&gt;dynamic class _String {&lt;br /&gt;	// Replace a string or substrings within a string&lt;br /&gt;	static function Replace (the_String, search_String, replace_String, occurrences, backward) {&lt;br /&gt;		if (search_String == replace_String) return the_String;&lt;br /&gt;		var found = 0;&lt;br /&gt;		if (backward == true) {&lt;br /&gt;			var pos = the_String.lastIndexOf(search_String);&lt;br /&gt;			while (pos&gt;= 0) {&lt;br /&gt;				found++;&lt;br /&gt;				var start_String = the_String.substr(0, pos);&lt;br /&gt;				var end_String = the_String.substr(pos + search_String.length);&lt;br /&gt;				the_String = start_String + replace_String + end_String;&lt;br /&gt;				pos = the_String.lastIndexOf(search_String, start_String.length);&lt;br /&gt;				if (found == occurrences) pos = -1;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		else {&lt;br /&gt;			var pos = the_String.indexOf(search_String);&lt;br /&gt;			while (pos&gt;= 0) {&lt;br /&gt;				found++;&lt;br /&gt;				var start_String = the_String.substr(0, pos);&lt;br /&gt;				var end_String = the_String.substr(pos + search_String.length);&lt;br /&gt;				the_String = start_String + replace_String + end_String;&lt;br /&gt;				pos = the_String.indexOf(search_String, pos + replace_String.length);&lt;br /&gt;				if (found == occurrences) pos = -1;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		return the_String;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	// Convert delimited (comma by default) String to an Array&lt;br /&gt;	static function toArray(string, separator:String) {&lt;br /&gt;		var list = new Array();&lt;br /&gt;		if (typeof(string) == "string"){&lt;br /&gt;			if (separator == undefined) separator = ",";&lt;br /&gt;			if (string == null) return false;&lt;br /&gt;			var currentStringPosition = 0;&lt;br /&gt;			while (currentStringPosition&lt;string.length) {&lt;br /&gt;				var nextIndex = string.indexOf(separator, currentStringPosition);&lt;br /&gt;				if (nextIndex == -1) break;&lt;br /&gt;				var word = string.slice(currentStringPosition, nextIndex);&lt;br /&gt;				list.push(word);&lt;br /&gt;				currentStringPosition = nextIndex+1;&lt;br /&gt;			}&lt;br /&gt;			if (list.length&lt;1) list.push(string);&lt;br /&gt;			else list.push(string.slice(currentStringPosition, string.length));&lt;br /&gt;		} else {&lt;br /&gt;			list.push(string);&lt;br /&gt;		}&lt;br /&gt;		return list;&lt;br /&gt;	}&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Tue, 13 Feb 2007 19:19:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3512</guid>
      <author>bgidge (Bryan Gidge)</author>
    </item>
  </channel>
</rss>
