PDA

View Full Version : creating functions (methods) for a movie clip?


carlhusic
01-09-2003, 03:51 AM
I'm using Flash 5 (not up to MX yet)

I've created a movie clip that I want to show and hide (using
movement and alpha fades) when certain buttons are pressed.

The "show" runs one portion of the clip containing a motion
tween that moves it onto the visible part of the movie and fades
the alpha to 100% until it hits a keyframe with stop() in it. The
"hide" runs another portion that does the reverse.

The control buttons (external to the movie clip) have the
appropriate gotoAndRun(framenum) calls in them, and they work
just fine to make the clip do it's thing.

In the spirit OOP, I'd like to create functions attached to the clip
(called "showMe()" and "hideMe()") so that I could change the
internals of the clip without the button needing to worry about
it. Basically the button code would look like:

on(press) {
_root.someClip.showMe();
}

So the question is - can I create functions like this? The movie
explorer seems to let me create functions on a movie clip instance
(using the "function") but for some reason, they seem to
disappear if I close the movie explorer and then go back to find
them!

Basically I'm treating the clips as objects and trying to make some
new methods for them. Is this possible at all? The follow up
question is, (if the answer to the above is true) is it possible to
attach functions/methods to the library object instead of the
instance in the movie, kind of like when you define methods when
you define a class in Java or C++?

If these things are not possible in Flash 5, how about MX?

jimburton
01-09-2003, 08:11 AM
you are on theright track - a function is a method of a particular object if it is defined within the scope of that object - for movieclips think of defining the function on the movieclip's timeline...so if you were to double-click on a clip to go into it's timeline, open the actions panel and type:

function sayHi() {
trace("hi!");
}

then you could call myClip.sayHi();

the other way to do it (which works fine in f5) is to define it from the main timeline, by going:

myClip.sayHi = function () {
trace("hi!");
}

which has the same effect....or you could add this method to every movieclip in the movie by adding to the movieclip prototype:

movieclip.prototype.sayMyName = function () {
trace(this._name);
}


the bit that mx adds on to this is that you can define event handlers (onpress etc) like any other function, from any scope, and remove them too. there is a really good intro to oop in flash which covers timelines and scope at
robin debreiul's site (http://www.debreuil.com/docs/ch01_Intro.htm)

farafiro
01-09-2003, 09:04 AM
check the function here also

http://moock.org/asdg/technotes/