AS3 Code Snippet
some snippets for AS3
replacing line breaks in actionscript (by ColinMook)
There’s a quick code snippet to replace windows-style line breaks (CRLF, or carriage return, line feed) with a standard actionscript new line character.
var newText:String = oldText.replace(/\r\n/g, "\n");
and here’s a snippet to replace all multiple line breaks with a single new line character:
var newText:String = oldText.replace(/[\r\n]+/g, "\n");
convert xml tags to lowercase (by ColinMook)
Here’s a handy little function for converting all tag names and attribute names in an XML object to lower case. Useful for doing case-insensitive matching on tag names and attribute names.
public function xmlTagsToLowerCase (xml:XML):XML {
// Convert the root tag to lowercase
xml.setName(xml.name().toString().toLowerCase());
for each (var attribute:XML in xml.@*) {
attribute.setName(attribute.name().toString().toLowerCase());
}
// Convert all descendant tags to lowercase
for each (var child:XML in xml..*) {
// If the node is an element...
if (child.nodeKind() == "element") {
// ...change its name to uppercase.
child.setName(child.name().toString().toLowerCase());
// If the node has any attributes, change their names to uppercase.
for each (attribute in child.@*) {
attribute.setName(attribute.name().toString().toLowerCase());
}
}
}
return xml;
}
//Usage:
var x:XML = xmlTagsToLowerCase(new XML ("<A C='D'><B>test</B></A>"));
trace(x); // Output: <a c="D"><b>test</b></a>
trim()
function trim(s:String):String {
return s ? s.replace(/^\s+|\s+$/gs, '') : "";
}
pad Zeroes
public function zeroPad(number:int, width:int):String {
var ret:String = ""+number;
while( ret.length < width )
ret="0" + ret;
return ret;
}
Trim Leading and Trailing 0′s From a String (by dougr.net)
private function trimLeading0sFromString() : void
{
var numericString:String = "00076925";
numericString = parseFloat ( numericString ).toString();
trace ( numericString ); // 76925
}
private function trimLeading0sFromString() : void
{
var alphaNumericString:String = "000076925-B72A32";
var pattern:RegExp = new RegExp ( '^0+' );
alphaNumericString = alphaNumericString.replace ( pattern,'' );
trace ( alphaNumericString ); // 76925-B72A32
}
private function trimTrailing0sFromString() : void
{
var alphaNumericString:String = "76925-B72A320000";
var pattern:RegExp = new RegExp ( '[0]+$' );
alphaNumericString = alphaNumericString.replace ( pattern,'' );
trace ( alphaNumericString ); // 76925-B72A32
}
Dynamic Speech Bubble (by razorberry.com)
/**
* SpeechBubble drawing code by A. Atkins (http://www.razorberry.com/blog/)
* Please retain this message if you re-distribute!
*/
package razor.graphics
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.geom.Point;
import flash.geom.Rectangle;
public final class SpeechBubble
{
/**
* Draw a speech bubble with the drawing API
* @param target The sprite in which to draw
* @param rect A Rectangle instance defining the position and size of the bubble
* @param cornerRadius The radius of the corners of the bubble (in px)
* @param point A Point instance defining the position of the point of the speech bubble.
*/
public static function drawSpeechBubble(target:Sprite, rect:Rectangle, cornerRadius:Number, point:Point):void
{
var g:Graphics = target.graphics;
var r:Number = cornerRadius;
var x:Number = rect.x;
var y:Number = rect.y;
var w:Number = rect.width;
var h:Number = rect.height;
var px:Number = point.x;
var py:Number = point.y;
var min_gap:Number = 20;
var hgap:Number = Math.min(w - r - r, Math.max(min_gap, w / 5));
var left:Number = px <= x + w / 2 ?
(Math.max(x+r, px))
:(Math.min(x + w - r - hgap, px - hgap));
var right:Number = px <= x + w / 2?
(Math.max(x + r + hgap, px+hgap))
:(Math.min(x + w - r, px));
var vgap:Number = Math.min(h - r - r, Math.max(min_gap, h / 5));
var top:Number = py < y + h / 2 ?
Math.max(y + r, py)
:Math.min(y + h - r - vgap, py-vgap);
var bottom:Number = py < y + h / 2 ?
Math.max(y + r + vgap, py+vgap)
:Math.min(y + h - r, py);
//bottom right corner
var a:Number = r - (r*0.707106781186547);
var s:Number = r - (r*0.414213562373095);
g.moveTo ( x+w,y+h-r);
if (r > 0)
{
if (px > x+w-r && py > y+h-r && Math.abs((px - x - w) - (py - y - h)) <= r)
{
g.lineTo(px, py);
g.lineTo(x + w - r, y + h);
}
else
{
g.curveTo( x + w, y + h - s, x + w - a, y + h - a);
g.curveTo( x + w - s, y + h, x + w - r, y + h);
}
}
if (py > y + h && (px - x - w) < (py - y -h - r) && (py - y - h - r) > (x - px))
{
// bottom edge
g.lineTo(right, y + h);
g.lineTo(px, py);
g.lineTo(left, y + h);
}
g.lineTo ( x+r,y+h );
//bottom left corner
if (r > 0)
{
if (px < x + r && py > y + h - r && Math.abs((px-x)+(py-y-h)) <= r)
{
g.lineTo(px, py);
g.lineTo(x, y + h - r);
}
else
{
g.curveTo( x+s,y+h,x+a,y+h-a);
g.curveTo( x, y + h - s, x, y + h - r);
}
}
if (px < x && (py - y - h + r) < (x - px) && (px - x) < (py - y - r) )
{
// left edge
g.lineTo(x, bottom);
g.lineTo(px, py);
g.lineTo(x, top);
}
g.lineTo ( x,y+r );
//top left corner
if (r > 0)
{
if (px < x + r && py < y + r && Math.abs((px - x) - (py - y)) <= r)
{
g.lineTo(px, py);
g.lineTo(x + r, y);
}
else
{
g.curveTo( x,y+s,x+a,y+a);
g.curveTo( x + s, y, x + r, y);
}
}
if (py < y && (px - x) > (py - y + r) && (py - y + r) < (x - px + w))
{
//top edge
g.lineTo(left, y);
g.lineTo(px, py);
g.lineTo(right, y);
}
g.lineTo ( x + w - r, y );
//top right corner
if (r > 0)
{
if (px > x + w - r && py < y + r && Math.abs((px - x - w) + (py - y)) <= r)
{
g.lineTo(px, py);
g.lineTo(x + w, y + r);
}
else
{
g.curveTo( x+w-s,y,x+w-a,y+a);
g.curveTo( x + w, y + s, x + w, y + r);
}
}
if (px > x + w && (py - y - r) > (x - px + w) && (px - x - w) > (py - y - h + r) )
{
// right edge
g.lineTo(x + w, top);
g.lineTo(px, py);
g.lineTo(x + w, bottom);
}
g.lineTo ( x+w,y+h-r );
}
}
}
Usage:
SpeechBubble.drawSpeechBubble(target:Sprite, rect:Rectangle, cornerRadius:Number, point:Point)
// Example:
var g:Graphics = graphics;
var m:Matrix = new Matrix();
m.createGradientBox(200,100,90*Math.PI/180,80,80);
g.clear();
g.lineStyle(2,0x888888,1,true);
g.beginGradientFill(GradientType.LINEAR, [0xe0e0e0, 0xffffff], [1,1], [1,0xff],m);
SpeechBubble.drawSpeechBubble(this, new Rectangle(80,80,200,100), 20, new Point(120,300));
g.endFill();
convert colors
function rgb2hex(r : uint, g : uint, b : uint, a : uint = 255) : uint
{
return (a << 24) | (r << 16) | (g << 8) | b;
}
function hex2rgb(color : uint) : Object
{
var c : Object = {};
c.a = (color >> 24) & 0xFF;
c.r = (color >> 16) & 0xFF;
c.g = (color >> 8) & 0xFF;
c.b = color & 0xFF;
return c;
}