Archive for July, 2010:
Rounding to nearest number
A very useful function to round and multiple by the rounding number to get you right back to where you were.
return Math.round(value/roundTo)*roundTo;
}
trace(roundToNearest(100, 12345)) //output 12300
Embedding fonts in AS3
Here is a good tutorial if your projects involve multi-language with different fonts. Using fontcontrol class it allow you to load fonts in dynamically in AS3. First create some SWF files that only contain the font combinations that you will need and then load them in to use using this FontManager class.
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
var _fontControl:FontControl;
function FontExample()
{
trace('new FontExample');
_fontControl = new FontControl();
_fontControl.addEventListener(FontControl.COMPLETE, _fontsLoaded);
_fontControl.load("font.swf", [ { font:"SimSun-ExtB", id:"SimSun" } ]);
}
function _fontsLoaded(e:Event):void
{
trace('font.swf loaded');
var tf:TextField = new TextField();
var fmt:TextFormat = new TextFormat(FontControl.FONTS[0].font, 20, 0x000000);
tf.autoSize = TextFieldAutoSize.LEFT;
tf.embedFonts = true;
tf.text = '您是不是要找 '+FontControl.FONTS[0].font;
tf.setTextFormat(fmt);
addChild(tf);
}
FontExample()
BitmapData checkPolicyFile
ActionScript 3 lets you specify if you want to load and check a cross-domain policy file when you load content from another server, by default policy files are not checked. To assure that Flash checks the necessary policy file when loading content, you would create a new LoaderContext instance, set its checkPolicyFile property (LoaderContext.checkPolicyFile) to true, and pass that instance into your call to load.
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
// use the context with your call to load
myLoader.load(request, context);
Query String Parameters
This example will show how to get the query string parameters with function.
var _queryString:String;
var _all:String = url;
var _params:Object = new Object();
var stringtoreturn:String
_queryString = url.split("?")[1]
if (_queryString) {
var allParams:Array=_queryString.split('&');
//var length:uint = params.length;
for (var i:int=0, index=-1; i < allParams.length; i++) {
var keyValuePair:String=allParams[i];
if ((index = keyValuePair.indexOf("=")) > 0) {
var paramKey:String=keyValuePair.substring(0,index);
var paramValue:String=keyValuePair.substring(index+1);
_params[paramKey]=paramValue;
if(para == paramKey) {
stringtoreturn = paramValue;
}
}
}
}
return stringtoreturn;
}
var uu:String = "http://www.youtube.com/watch?v=FzRH3iTQPrk&playnext_from=TL&videos=zmpLVe0KGn8"
trace(readQueryString(uu, "v")) //FzRH3iTQPrk
