Skip to content

Instantly share code, notes, and snippets.

View geraldyeo's full-sized avatar
🎯
Focusing

Gerald Yeo geraldyeo

🎯
Focusing
View GitHub Profile
@geraldyeo
geraldyeo / fastIsNaN.as
Created November 3, 2010 09:01
very fast isNaN()
private function myNewIsNaN(val:Number): Boolean
{
return val != val;
}
val != val; // inline isNaN()
@geraldyeo
geraldyeo / selectOnFocus.as
Created November 9, 2010 04:26
Automatically select all text on focus
titleTF.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
titleTF.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
function onFocusIn(event:FocusEvent):void {
setTimeout(event.target.setSelection, 50, 0, event.target.text.length);
}
function onFocusOut(event:FocusEvent):void {
event.target.setSelection(0,0);
}
@geraldyeo
geraldyeo / textfieldEnter.as
Created November 9, 2010 04:39
Enter key prevention in multiline textfield
textfield.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
private function keyHandler(event:KeyboardEvent):void {
if(event.type == KeyboardEvent.KEY_UP && event.keyCode == Keyboard.ENTER) {
event.target.text = event.target.text.replace("\r", "");
}
}
@geraldyeo
geraldyeo / embedFlash.html
Created November 9, 2010 04:46
Embedding Flash - XHTML valid and no js
<object data="stage.swf" width="400" height="300" type="application/x-shockwave-flash" style="width:400px; height:300px;">
<param name="movie" value="stage.swf"/>
</object>
@geraldyeo
geraldyeo / replace-line-breaks
Created January 4, 2011 03:42
REPLACING LINE BREAKS IN ACTIONSCRIPT
// here'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");
// for example, the preceding code would change the following string:
// a
//
@geraldyeo
geraldyeo / snippets
Created February 10, 2011 05:50
super-fast operations
var max:Number = Math.max.apply(null, randomList);
/*
An array's push method is capable of taking any number of arguments (meaning statements like this are valid: array.push(1,2,3,4)). Thus, if we call that method using apply and put in the sliced array all of the resulting items will be "concatenated" onto the end of the array, in one, single super-fast operation.
*/
array.push.apply( array, array2 );
@geraldyeo
geraldyeo / bitwise.as
Created March 28, 2011 06:53
Bitwise gems – fast integer math
/*
Left bit shifting to multiply by any power of two
Approximately 300% faster.
*/
x = x * 2;
x = x * 64;
//equals:
x = x << 1;
@geraldyeo
geraldyeo / useful_functions.as
Created April 4, 2011 02:26
various useful as3 functions
// tinting
var colorTransform : ColorTransform = bmp.transform.colorTransform;
colorTransform.color = 0x3b1c4d;
bmp.transform.colorTransform = colorTransform;
// get URL of page where swf is embedded
function get currentURL():String
{
var url:String;
if (ExternalInterface.available) {
@geraldyeo
geraldyeo / fastTrig.as
Created May 24, 2011 03:52
Fast and accurate sine/cosine approximation
//1.27323954 = 4/pi
//0.405284735 =-4/(pi^2)
/*********************************************************
* low precision sine/cosine
*********************************************************/
//always wrap input angle to -PI..PI
if (x < -3.14159265)
x += 6.28318531;
@geraldyeo
geraldyeo / as3_clicktag.as
Created July 27, 2011 09:45
as3 clicktag
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
var clickTAG:String = String(paramObj["clickTAG"]);
box_mc.addEventListener(MouseEvent.CLICK, openBanner);
function openBanner(event: MouseEvent) : void {
flash.net.navigateToURL(new URLRequest( clickTAG ), "_blank");
}
box_mc.useHandCursor = true;