Skip to content

Instantly share code, notes, and snippets.

@Nutrox
Nutrox / Oscillator.as
Created December 11, 2010 16:38
AS3 Oscillator Class
package nx.audio {
/**
*/
public class Oscillator implements ISampleGenerator {
private static const WAVEFORM_PULSE:uint = Waveform.PULSE;
private static const WAVEFORM_SAWTOOTH:uint = Waveform.SAWTOOTH;
private static const WAVEFORM_SINE:uint = Waveform.SINE;
private static const WAVEFORM_TRIANGLE:uint = Waveform.TRIANGLE;
@Nutrox
Nutrox / isDefinition.as
Created December 18, 2010 22:25
AS3 isDefinition() function
package {
import flash.utils.describeType;
//
// trace( isDefinition(Sprite,"flash.display::Sprite") ); // true
// trace( isDefinition(Sprite,"flash.display::InteractiveObject") ); // true
// trace( isDefinition(Sprite,"flash.events::EventDispatcher") ); // true
// trace( isDefinition(Sprite,"flash.events::Event") ); // false
//
public function isDefinition( definition:Class, type:String ):Boolean {
var info:XML = describeType( definition );
@Nutrox
Nutrox / AbstractExample.as
Created January 3, 2011 20:43
AS3 Abstract-esque class implementation
package {
public class AbstractExample {
public function AbstractExample() {
if( Object(this).constructor == AbstractExample ) {
throw new Error( "AbstractExample class must be extended." );
}
}
}
}
@Nutrox
Nutrox / count_bytes.php
Created January 5, 2011 00:25
PHP - Counts the number of bytes in a string, regardless of any mbstring overloading.
// Counts the number of bytes in a string.
function count_bytes( &$str ) {
if( ini_get( 'mbstring.func_overload' ) ) {
return mb_strlen( $str, '8bit' );
}
// Ideally we want strlen() to be used, because it's fast.
return strlen( $str );
}
@Nutrox
Nutrox / StringUtil.replace.as
Created January 13, 2011 18:56
AS3 - String replacement utility function.
function replace( str:String, search:String, replacement:String ):String {
var a:int = search.length;
var b:int = replacement.length;
var o:int = 0;
var i:int;
while( ( i = str.indexOf( search, o ) ) != -1 ) {
str = str.substr( 0, i ) + replacement + str.substr( i + a );
o = i + b;
}
return str;
@Nutrox
Nutrox / gist:778619
Created January 13, 2011 21:14
AS3 - Isometric matrix transformation.
const DEGRAD:Number = Math.PI / 180;
var iso:Matrix = new Matrix();
iso.rotate( -45 * DEGRAD ); // rotate anti-clockwise by 45 degrees
iso.scale( 1.0, 0.5 ); // scale vertically by 50%
iso.translate( 100, 100 ); // set position if needed
displayObject.transform.matrix = iso;
@Nutrox
Nutrox / gist:779208
Created January 14, 2011 05:18
AS3 - Random capital letter.
const CHR:Array = [
"A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
];
const CHRLEN:Number = CHR.length;
function getRandom():String {
return CHR[ CHRLEN * Math.random() >> 0 ];
}
@Nutrox
Nutrox / gist:780784
Created January 15, 2011 08:36
PHP - JSONObject class.
class JSONObject extends stdClass {
// Accepts an array, object, or JSON encoded string.
public function __construct( $o=null ) {
if( $o === null ) {
return;
}
if( is_string( $o ) ) {
$o = json_decode( $o );
}
@Nutrox
Nutrox / gist:789900
Created January 21, 2011 16:14
JavaScript - String.trim()
new function() {
var TRIM = /^\s*(.+?)\s*$/;
String.prototype.trim = function() {
return this.replace( TRIM, '$1' );
}
}
@Nutrox
Nutrox / gist:791176
Created January 22, 2011 15:22
JavaScript - DOM Ready
//
// Works in Chrome, Firefox, MSIE, Opera and Safari.
//
function onDOMContentLoaded( event ) {
if( event ) {
document.removeEventListener( event.type, onDOMContentLoaded, false );
}
document.onreadystatechange = null;
// do something here...
}