Skip to content

Flash: Create MovieClip With Custom Class

Categories: Uncategorized

Table of Contents

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
[code lang=”actionscript”]
/*
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;
}
[/code]

Use it just like attachMovie except you specify a class for the first parameter, eg:
[code]createClassMovieClip(CustomClass, “new_clip”, getNextHighestDepth(), {_x:10, _y:10, something:function(){this.something = 10;}})[/code]
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!

Keep in Touch

Subscribe to my email list to keep in touch. I’ll send you new blog posts and other thoughts.