Skip to content

Extentions everywhere!

September 30, 2006

In the last post I destribed the basic concept of the Commaner engine. I must say that I was quite dissapoineted because this week I didn’t eat the treditional friday schintzel launch with my family…

So I’ve made this classes that have a paralel hirarchy to the movieclip hirarchy. Witch meens that it has _parent, and _children and can attach or create or wrap a child and all the work is being done behind the stage. But what is all the work for? Why can’t I just use the movieclip itself?
I wanted to make any movie clip take the custome behaviours I made for it. Extending a movieclip would make it much more difficult to apply these when attaching, creating and wrapping movie clips.

What are these extentions?
A MovieClipCommander extention is a class that extends MovieClipService. By extending this class, you created a service: a class that is useless by itself, but has a strong link to a commander, its assosiated movie clip, and other services that are registred on that commander.

Take a simple Drag and Drop extention. All it does, is listen to “onPress”, then starts moving the clip with “onMouseMove” and stop when “onMouseUp”.
Services has no constructor, instead they use “init” function. This is due to the internal structure of the servise system. The “init” function can be private, that way preventing accidental call to it. In the init function all needs to be done is set the listening to the press function. Let see the code with some inline comments:
(I didn’t write the packages and stuff, I think this information is irrelevent for now)
class Drag extends MovieClipService {
.
.private function init() {
.
..// listenning to the clip "onPress" event.
..//Other users of that class can still
..// listen to that event without problems
.
..__commander.addClipEvent("onPress", this);
.}
.
.// event handler function is a function
.// with the name of the event
.// can be private to prevent calling
.// from outside
.
.private function onPress(evt:Object) {
..__commander.addClipEvent("onMouseMove", this);
..__commander.addClipEvent("onMouseUp", this);
..__commander.removeClipEvent("onPress", this);
.}
.
.private function onMouseMove(evt:Object) {
.
..// the programmer can rest assure that
..// the clip exist - it is there.
..// the commander takes care of
..// making the correct clip available.
.
..__clip._x == __clip._parent._xmouse;
..__clip._y == __clip._parent._ymouse;
.}
.
.private function onMouseUp(evt:Object) {
..__commander.removeClipEvent("onMouseMove", this);
..__commander.removeClipEvent("onMouseUp", this);
..__commander.addClipEvent("onPress", this);
..dispatchEvent("stopDrag", {x:__clip._x, y:__clip._y});
.}
}

Thats it. No more byrocracy and tricks to make these things work. Use of the Drag extention is as easy as it gets:
// this operation wraps an existing
// movie clip called "clipName".
// the constructor can wrap a refference,
// a clip name in a refference, create empty
// one on a refference, or
// attach a movie clip
.
var newMcc:MovieClipCommander = new MovieClipCommander(_root, "clipName");
newMcc.registerExtention("drag", Drag);
.
// the event that the Drag dispatches is actually dispatches from the MovieClipCommander
.
newMcc.addEventListener("stopDrag", this);

I appologize for the dots, the blog get messy without them.

And here is a working example from very early stages of the framework. This was post if Flashoo, the best Israeli flash community. The rectangle is draggable. Refresh the page if it gets out of the view.

Till next Time. Have fun programming. 🙂

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: