Never been to CodeSnippets 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 (or not, you can keep them private!)

Take a snapshot from the Flash Player

Take a snapshot of a DisplayObject rendered inside the Flash Player and have the byte array available for server side processing or for reuse inside the Flash application.

import com.adobe.images.*;
import com.dynamicflash.util.Base64;
import flash.display.DisplayObject;
import flash.geom.Rectangle;
import flash.utils.ByteArray;

function capture(target:DisplayObject, options:Object, format:String):void{

	var relative:DisplayObject = target.parent;

	var rect:Rectangle = target.getBounds(relative);
			
	var bitmapData:BitmapData = new BitmapData(rect.width, rect.height);
			
	bitmapData.draw(relative, new Matrix(1, 0, 0, 1, -rect.x, -rect.y ));
			
	var byteArray:ByteArray;

	switch (format){
		
		case "JPG":
		var jpgEncoder:JPGEncoder = new JPGEncoder(JPG_QUALITY_DEFAULT);
		byteArray = jpgEncoder.encode(bitmapData);
		break;
				
		case "PNG":
		byteArray = PNGEncoder.encode(bitmapData);
		break;

	}
			
	var byteArrayAsString:String = Base64.encodeByteArray(byteArray);

	// Send the data to the server

	

roundTo function in Actionscript

This function takes an input value and rounds it to the nearest value of the rTo parameter.

function roundTo(num:Number,rTo:Number):Number
{
	return Math.round(num / rTo) * rTo;
}


Occasionally there is a rounding error and you get a long string of decimals of 0's or 9's followed by another number. I can't figure out a way of changing this.

This can also be changed to floorTo or ceilTo depending on what you need.

Main class

//
class clases.Main
{
public static var mcMain : MovieClip = new MovieClip ();
//
//
// Constructor que activa la clase
//
public function Main (mainMC : MovieClip)
{
mcMain = mainMC;
}
//
//
// Capturar el XML de los productos
//
public function getXML () : Void
{

}

}

Get your Flash Movie's URL

This function returns the full URL of the .swf file. Simple enough.

myUrl = getProperty("", _url );

Finding stuff in strings / or not!

Contains is an actionscript function that I use a lot. From Kirupa

contains = function (input, arrayData) {
  for (i=0; i<arrayData.length; i++) {
    if (arrayData[i] == input) {
      return 1;
    }
  }
  return -1;
};

explode() function for Actionscript

A simple version of the explode() function in PHP. It takes a string and splits it up into an array by splitting it at whatever character (or characters) you specify. For example, reading in a tab delimited text file. Will split it into lines by splitting on returns ("\r"). Then split up the lines by splitting on tabs ("\t").

Attribution: I didn't write this myself, I found it in a comment on one of the Actionscript on-line documentation pages.

function explode(separator:String, string:String) {

	var list = new Array();

	if (separator == null) return false;
	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));
	}
	return list;
}

print_r() for Actionscript

Here's a cheap little function to mimic the print_r() function from PHP in Actionscript. It's designed to work on most any array and will handle nested arrays through recursion.
//
// recursive function to print out the contents of an array similar to the PHP print_r() funciton
//
function print_a( obj, indent ) {
	if (indent == null) indent = "";
	var out = "";
	for ( item in obj ) {
		if (typeof( obj[item] ) == "object" )
			out += indent+"[" + item + "] => Object\n";
		else
			out += indent+"[" + item + "] => " + obj[item]+"\n";
		out += print_a( obj[item], indent+"   " );
	}
	return out;
}
// example call
trace( print_a( example_array ) );

Collapse Whitespace function for Actionscript

This function will strip out all tabs and return characters. It will also collapse all runs of multiple spaces down to one. It will also remove a leading and trailing spaces. It is similar to the php_strip_whiespace() function in PHP.

function collapseWhiteSpace( theString ) {
	theString =  theString.split("\r").join("");
	theString =  theString.split("\t").join("");
	while ( theString.indexOf("  " ) != -1 ) {
		theString= theString.split("  ").join(" ");
	}
	if (theString.substr(0,1) == " ") {
		theString = theString.substr( 1 );
	}
	if (theString.substr( theString.length-1, 1 ) == " ") {
		theString = theString.substr( 0, theString.length - 1 );
	}
	return( theString );
}

botones con cambio de frame adentro de movieclip

this.on_btn.onRelease=function(){
gotoAndStop("off");

}
this.off_btn.onRelease=function(){
gotoAndStop("on");
}

stoping a movie clip

/*---------------------------
Tengo un botony un movie clip que
avanza y despues el boton lo para


*/
volver.onPress = function(){
violeta.onEnterFrame=function(){
this._x+=27 ;
}
}
boton1.onPress = function() {
delete violeta.onEnterFrame;
}