A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Custom Method in MovieClip

  1. #1
    Junior Member
    Join Date
    Mar 2000
    Posts
    9
    I've been fascinated by my study of OO programming in Flash. I'm *beginning* to get it.

    Here's one connection I haven't been able to make.

    Let's say I created a MC (called MyMC). Using the OLD way, if I wanted to toggle the animation of this MC on or off, I would do something like (from a holderMC),

    If (IsSpinning) {
    this.MyMC.gotoandstop("lblStop");
    } else {
    this.MyMC.gotoandplay("lblAnimate");
    }

    (I may not have the syntax exactly right, but that's not what I'm after right now.)

    I don't want to have to put that code in each holder MC. Instead, I want to build a custom method into MyMC (The class, not the instance). So, in the holder MC, I could call the method to toggle animation:

    this.MyMC.ToggleAnimation

    And, the method inside MyMC would run code similar to the If statement above, and toggle itself.

    Is this possible, and if so, how do I do it?

    Nothing I have read, so far, about custom methods and prototypes explains how to do it to an existing MC, only on how to do it when building a custom object.

    I'm sure there is a simple connection, but I just can't find it.

    Thanks in advance for your help. (This is a GREAT resource!)

    -Keith

  2. #2
    Senior Member
    Join Date
    Sep 2001
    Posts
    122
    you'll want to either write that method as a MovieClip method, or set the class of your clip to MyMc.

    i'm sure that the names you used are just example names, but you should be careful naming clips and classes the same thing, it'll lead to problems.

    this is a subject i find quite hazy too, so instead of a drawn out and mostly inaccurate attempt at telling you how i would do it, i'll point you to people who actually know how.

    http://www.quantumwave.com/html/flashcode.html
    http://www.debreuil.com/docs/ch01_Intro.htm

    happy reading...

  3. #3
    Junior Member
    Join Date
    Mar 2000
    Posts
    9

    Already been there.

    Yes, those are excellent resources. I read both of those before posting. It may be in there, but I just can't find how to create a method (function) inside a MC and then call it from outside the MC.

    It seems to me that I ought to be able to place the function in the first frame of the MC, and then call it from anywhere else, using the approprate path.

    I don't even know if I need to "Prototype". And, if I do, I don't see how I can do that from within the MC. That's where I'm lost.


    you'll want to either write that method as a MovieClip method,
    Would that be a method of the specific MC class? or of the general MovieClip class?


    or set the class of your clip to MyMc.
    How do I do that?

    -Keith

  4. #4
    Senior Member
    Join Date
    Sep 2001
    Posts
    122
    here's how dave yang at quantum wave has done it:
    Code:
    function Circle(r) {
       this.class = "Circle";
       this.radius = r;
    }
    
    Circle.prototype.area = function() {
       return this.radius * this.radius * Math.PI;
    };
    
    // top-level superclass must link back to MovieClip.prototype
    Circle.prototype.__proto__ = MovieClip.prototype;
    
    MovieClip.prototype.setClass = function(c, args) {
       this.super = c;
       this.super(args);
       delete this.super;
    
       this.__proto__ = c.prototype;
    };
    then to make mymc a circle you'd go:
    Code:
    mymc.setClass(Circle);
    admittedly, i'm a little unsure of just how this works, but i've used it successfully nonetheless! (thanks dave).

    good luck!

  5. #5
    Junior Member
    Join Date
    Mar 2000
    Posts
    9

    here's how dave yang at quantum wave has done it:
    Well, that's my point exactly. He shows how to do methods and prototypes of a custom object (circle) that is created via code.

    But, what if there was a MC in the Library called, "MCspinningBall"?

    And, what if you wanted that MC to have a method that turned the spinning on or off (or to get more complex -- to bounce)?

    What code would you create for those methods? And, where would you put them? And, finally, how would you call them from outside the MC instance?

    -Keith

  6. #6
    Senior Member
    Join Date
    Sep 2001
    Posts
    122
    i put all my methods etc, in frame 1 of _root. you want to be sure that your methods are not being defined over and over again. ie. if you put them in frame 1 _root, don't revisit that frame.

    if you had MCspinningBall in the library, you would put it on the stage as a circle like so:

    _root.attachMovie("MCspinningBall","somegreatname" ,5);
    somegreatname.setClass(Circle,5);

    now somegreatname has access to any methods you've defined for class Circle. so writing:

    somegreatname.area();

    will return 78.5398..... since you passed 5 as the radius.

    clearer?

    actually, attempting to explain this is making it much clearer for me!

  7. #7
    Junior Member
    Join Date
    Mar 2000
    Posts
    9

    Closer...

    i put all my methods etc, in frame 1 of _root. you want to be sure that your methods are not being defined over and over again. ie. if you put them in frame 1 _root, don't revisit that frame.
    I assume that's _root of the mc in question? (ie MCspinningBall referenced below?)


    if you had MCspinningBall in the library, you would put it on the stage as a circle like so:

    _root.attachMovie("MCspinningBall","somegreatname" ,5);
    somegreatname.setClass(Circle,5);
    Is there no way to make this work w/o "attachMovie"? Can this be done with instances that are dragged from the Library?


    now somegreatname has access to any methods you've defined for class Circle. so writing:

    somegreatname.area();

    will return 78.5398..... since you passed 5 as the radius.

    clearer?
    Yes, I get it when it relates to creating objects programatically. Just not when you create instances by dragging them onto the stage.

    Sorry I'm being dense. If the methods (functions) are in frame 1 of MCspinningBall, why must a class be assigned to instances of MCspinningBall? And, is it possible to assign a class to a dragged instance? And, if so, how?

    -Keith



    actually, attempting to explain this is making it much clearer for me!
    Glad to help! []
    [Edited by kaprice on 02-02-2002 at 09:42 PM]

  8. #8
    Senior Member
    Join Date
    Sep 2001
    Posts
    122
    i meant _root as in _root of the whole thing, _root, _level0.

    if mymc already exists on the stage (ie. you dragged it there), have it call the setClass() method just like you would any other MovieClip method (like play(), stop(), etc). that will make it a Circle and give it access to the Circle prototypes that you've defined, wherever you've defined them.

    if you define a function in frame 1 of mymc, then that function will exist in that movie and there's no need to set the class to gain access to it. but that function will only exist in that movie, and for other movies to use it, they'll need to path to it's location, _root.mymc.customFunction(). that's not so much oop, that's a static function.

    if you define a method in frame 1 of mymc, that method will exist for all members of the class you defined it in, and no pathing will be necessary. however, that method won't exist until the playhead hits frame 1 of mymc and it runs that code. if mymc is on the stage at the beginning of the movie that'll be right away, but if you attach mymc later on that method won't be defined until then.

    that's why i put everything in frame 1 of _root, so i know that all my methods are available right off. why mess around?

  9. #9
    Junior Member
    Join Date
    Mar 2000
    Posts
    9
    Thanks again.

    It may not be OO, but I'm pleased with what I came up with.

    It turns out my main problem was with syntax. Here's what works (in frame 1 of mymc):

    fSpinning = false // Init

    function ToggleSpinner () {
    if (fSpinning) {
    stop();
    } else {
    play();
    }
    fSpinning = not fSpinning;
    }

    Then, from any mc instance that has a mymc instance in it (instMyMC), I can:

    this.instMyMC.ToggleSpinner();

    It works beautifully.

    Here's what I had previously tried that was NOT working (in frame one of mymc):

    ToggleSpinner = function() {
    ...
    }

    I later tried:

    function ToggleSpinner(){
    ...
    }

    which didn't work because I had no space between () and {.

    So, OO or not, I'm happy with it. The code that makes the mc work is inside the mc. I can call the code as if it were a method (even if it isn't). And, it all works with dragged instances.

    Thanks for all your input.

    -Keith

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center