How to use flashvars in as3

I use flashvars heavily to create customizable swfs. After some time I got tired of copy pasting my code and found this:

[as3]
function getFlashVars():Object {
return Object(LoaderInfo(this.loaderInfo).parameters);
}
[/as3]

I only need to call this method (which returns an object) and chain it with the flashvar I want. This only works on timeline code though.

Here’s how I use it:

[as3]
strXMLPath = getFlashVars().xmlPath || "slideshow.xml";
[/as3]

The || is for error handling. If I can’t get the flashvar:xmlPath, I created a default file slideshow.xml to show default data.

Advertisements

How to use dynamic fonts on html using AS3

Challenge:
Most designers find themselves worrying about cross-browser compatibility. Most of the time, they have to work with a limited choice of fonts.

What if you want to do something flashy, like this: http://us.blizzard.com/en-us/company/

Notice the gradient and the drop shadow in the “Company” header. It’s done in flash. And since it’s Blizzard, I assume it runs under a CMS so it’s dynamic. You can change “Company” to “Kompanya” just as easily.


Solution:


1. Design whatever wonderful dynamic text you want in flash.


2. Make sure you embed the characters you want included in the font.


3. Assuming your instance name is “textMC”. Put this in the main timeline:

[as3]

var params:Object = LoaderInfo(this.root.loaderInfo).parameters;
var _text:String = String(params["content"]);
var _orientation:String = String(params["align"]);

var myTextFormat:TextFormat = new TextFormat();
if(_orientation == "center"){
myTextFormat.align = TextFormatAlign.CENTER; //align center
}else if(_orientation == "right"){
myTextFormat.align = TextFormatAlign.RIGHT; //align right
}else{
myTextFormat.align = TextFormatAlign.LEFT; //default alignment is left
}

textMC.text = _text;
textMC.setTextFormat(myTextFormat);

[/as3]


4. In your embed code, specify “content” and “align” in the params. “content” contains your content. If you want to set the alignment set “align” to “center” or “right”. The text aligns to the left by default. Ex:

[as3]
content="This is the content"
[/as3]

Note:
Call setTextFormat() only after you set the text. Flash needs to know the content first, before it can properly format the textfield.

How to embed fonts properly in AS3

Best practice for embedding fonts in AS3.

Fonts can be embedded using the Flex metadata “Embed”.

For example, if you want to embed the font named Arial (this should be the system font located in FontBook or the Fonts folder)

[as3]
[Embed(systemFont="Arial", fontName="Arial", mimeType="application/x-font", unicodeRange="U+0041-U+0054")]
var arial:Class;

Font.registerFont(arial);

[/as3]

systemFont: looks for the current system font with the name “Arial”

fontName: the name used to reference this font inside the code

mimeType: the type of file that i’m embedding, in this case an x-font

unicodeRange: the sample unicode range represents uppercase characters from A-Z (convert unicode easily here)

For best practices, we can compile the file as a swf, and load them at runtime using the loader class. This way we only need to load the file when it’s actually needed.

How to add a favicon

1. Create an icon file. 16×16 is the normal size although 36×36 icons will be resized by the browser. A .gif can also be used.

2. Add this code inside the “head” : <link rel=”icon” href=”image/favicon.ico” type=”image/ico”/> 
3. The href should specify which directory the icon is located

Get an XML in jquery

$.ajax({
type:”GET”,
url:”story.xml”,
dataType: “xml”,
success:function(xml){
                     $(xml).find(‘line’).each(function(){
                         var id_text = $(this).attr(‘id’)
                         var name_text = $(this).find(‘name’).text()   
storyArray[$(this).attr(‘id’)] = $(this).text();                  
                     });               
}
});

Open a file and output numbers

#include
#include
void accessFile();
int main()
{
    accessFile();
    return 0;
}
void accessFile()
{
    FILE *fp;
    int n = 0;
    int z = 0;
    fp = fopen(“count.txt”,”a+”);
    if(fp == NULL)
        printf(“Error opening file”);
    else
        {
            while(!feof(fp))
            {
                fscanf(fp, “%d”, &z);
                n++;
            }
        }
    fprintf(fp,”%dn”,n-1);
}

A More Complicated Preloader

Steps for complicated pre-loading:

1. The actual movie starts on the 3rd frame. The 1st frame is for the preloader. The 2nd frame is for loading the codes and stuff

2. To set the 2nd frame as the part to load the bulks, set Publish Settings>Flash>Settings>Export Frame for Classes, set it to 2.

3. Set the linkage of library symbols to not load on first frame.

4. Create a preloader graphics and put it into the first frame.

5. Add actionscript in to the main timeline.

this.onEnterFrame = function()
{
var amount:Number = this.getBytesLoaded()/this.getBytesTotal() * 100;
preloader._xscale = amount;

if(amount == 100)
{
preloader._alpha-=5;
if(preloader._alpha < 0)
{
this.gotoAndPlay(3);
delete this.onEnterFrame;
}
}
}