Monthly Archive for May, 2007

Flash: Create MovieClip With Custom Class

I’ve always wanted to attach a movieclip to the stage with a class other than MovieClip without having to use swfmill or the ide to create a movieclip with linkageID + custom class. Well that day is finally here! I found a nice snippet of code on the net and modified it a bit to act more like attachMovie (ability to specify an initOb). The result?

createClassMovieClip

:s

/*
 Description: A function used for creating empty movie clips with subclass association.
 
 Parameters:
 c:Function - The class to associate with the the empty movie clip.
 name:String - The instance name for the empty movie clip.
 depth:Number - The depth for the empty movie clip.
 initOb:Object - Optional, object to copy properties from
 
 Returns:
 MovieClip - A reference to the newly created movie clip.
 */

MovieClip.prototype.createClassMovieClip = function(c:Function, name:String, depth:Number, initOb:Object) : MovieClip {
    var mc:MovieClip = this.createEmptyMovieClip(name, depth);
    mc.__proto__ = c.prototype;
    mc.constructor = c;
   
    if(initOb) {
        for(var prop in initOb) {
            mc[prop] = initOb[prop];
        }
    }
   
    c.call(mc);
    return mc;
}

Use it just like

attachMovie

except you specify a class for the first parameter, eg:

createClassMovieClip(CustomClass, "new_clip", getNextHighestDepth(), {_x:10, _y:10, something:function(){this.something = 10;}})

Pretty slick!

Snipplr

Snipplr is a neat site I came across a couple months ago but forgot to post about. It a nicely designed web-2.0-ish site that allows you to post and view code snippets for various different languages (Cocoa, PHP, HTML, CSS, etc). It even has cloudtags and TextMate integration! I’ve been posting some smaller snippets on that site instead of the source code page, so go and check it out.

In the same vein, Code Beach was recently launched. Code Beach is basically the same idea as Snipplr, except it is focused solely on Cocoa based code snippets. It’s nice to see more of these code sharing sites cropping up, they save alot of time!

Flash & Fonts

I’ve written about the pains of fonts in flash before; and it something that still plauges my work today. However, since that post new tools have been created, new workflows introduced, and with that some new findings.

Fonts With SWFMILL

SWFMILL is an awesome tool that may allow you to leave the Flash IDE untouched for some projects. However, embedding fonts seems to be scarcely documented (I had trouble getting the methods documented working). Here are the two links I’ve found that document embedding fonts with swmill:

I wasn’t able to get embedded fonts working using the information above, so here is my method. This is what my swfmill xml looks like.

FontFile.ttf must be referenced relative to the location of the swfml file, and must be a TrueType font for swfmill to read it successfully. If you want to use a non-TrueType font convert it to a TrueType font with a tool such as FontForge (works great and its free, but has a horrific UI).Now if you create a NavButton class that looks something like this:

class NavButton extends MovieClip {
private var oTitle:TextField;

function NavButton() {
super();

oTitle.selectable = false;
oTitle.multiline = false;
oTitle.wordWrap = false;
oTitle.textColor = 0xFFFFFF;
oTitle.autoSize = true;
oTitle.text = "This is a title";
}
}

And attach the NavButton to the stage using

attachMovie()

you should see the text come up. Note: you don’t need to set

oTitle.embedFonts = true;

to make this work correctly! Actually, dont use

embedFonts

at all!

You may have noticed the properties that I had to set on

oTitle

, this will be different for every situation. It seems that swfmill sets a bunch of default properties when creating a textfield. As a reference, here is the output from

dumpObject(oTitle)

(dumpObject() is a method from my debug class):

styleSheet:undefined
mouseWheelEnabled:true
condenseWhite:false
restrict:null
textHeight:48
textWidth:54
bottomScroll:1
length:12
selectable:true
multiline:true
password:false
wordWrap:true
background:false
border:false
html:false
embedFonts:true
maxChars:null
maxhscroll:0
hscroll:0
variable:null
htmlText:hello world!
type:input
text:hello world!
autoSize:none
tabIndex:undefined
textColor:0
backgroundColor:16777215
borderColor:0
maxscroll:2
scroll:1
filters:undefined
sharpness:undefined
thickness:undefined
antiAliasType:undefined
gridFitType:undefined

Shared Fonts in Flash IDE

Shared fonts, especially shared pixel fonts, are tricky business in the Flash IDE. Sometimes I wonder if it’s worth the effort, and whether I should just embed the fonts in each SWF. Alot of times I get blurred text when loading the pixel font from a shared library, but no blurring if the font is embedding in the swf itself. Shared fonts seem to work well with non-pixel fonts though; so if you dont need pixel fonts I would definitly use shared libraries. If you are looking at using shared fonts in your project I would recommend checking out Shared Fonts Manager.