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.

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;
}
}
}

How to disable people from forward-ing in flash movies

var myMenu:ContextMenu = new ContextMenu();
myMenu.hideBuiltInItems();

Turns out that thing that pops up when you right click a movie is called a ContextMenu. So an easy way to disable it is to hide them by using hideBuiltInItems();

An item in the ContextMenu is called a ContextMenuItem.

var useless:ContextMenuItem = new ContextMenuItem(“Useless Me”, deadClick);

The 1st parameter is the label of that item. The 2nd is what function it calls if the item was clicked.

function deadClick () {

}

We just create an empty function for it. If we want to make the menu item more useful make it call a function that does something cool.

function gotoMySite () {

getURL(“http://codeanginamo.blogspot.com/&#8221;, “_top”);

}

You can add items into a menu by pushing it into the customItems stack of the ContextMenu.

myMenu.customItems.push(mySiteLink, copyrightNotice);

Then we attach it into the main timeline:

_root.menu = myMenu;

A linked list

/*#include headers here */

struct list_el {
int val;
struct list_el * next;
};

typedef struct list_el Item;

Item *list, *tail; //references to the linked list

int main() {

int i;
Item *curr;
int array[] = {1, 24,3,412,125,6,74,228,9, 10};

/*initialize the list*/
list = (struct Item*)malloc(sizeof(Item));
list->next=NULL; // this is important to
list->val = 0; // avoid wierd looking variables occupying our list

tail = list;

/*populate the list*/
for(i = 0; i
{
addToList(array[i]);
}

curr = list;
while(curr->next != NULL)
{
printf(“%d -> “, curr->val);
curr = curr->next;
}
printf(“%dn”, tail->val); //the tail isn’t really printed coz a tail triggers the loop to end
}

addToList(int value)
{
Item *curr;

if(tail == list && tail->val == NULL) //if the list is empty
{
tail->val = value; //create the head, since it’s the only element
tail->next = NULL; //it is also the tail
}
else
{
if(curr = (struct Item*)malloc(sizeof(Item)))
{
curr->val = value; //create a new node
curr->next = NULL; //new node becomes the tail — essentially, coz it doesn’t have anything after it
tail->next = curr; //the old tail links with the new node, so essentially it’s no longer a tail
tail = curr; //the new node is now formally a tail
}
else
{
printf(“nOut of memory errorn”);
system(‘pause’);
}
}
}