RSS

Rounding to nearest number

Actionscript 3 | Posted on Jul 27 2010

A very useful function to round and multiple by the rounding number to get you right back to where you were.

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

trace(roundToNearest(100, 12345)) //output 12300

Embedding fonts in AS3

Actionscript 3 | Posted on Jul 27 2010

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 com.sitedaniel.text.FontControl;
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()

Download the source code

BitmapData checkPolicyFile

Actionscript 3 | Posted on Jul 26 2010

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.

// create a loader context that checks for policy files
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;

// use the context with your call to load
myLoader.load(request, context);

Query String Parameters

Actionscript 3 | Posted on Jul 26 2010

This example will show how to get the query string parameters with function.

function readQueryString(url:String, para:String) {
    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