Skip to main content
Jon Chambers
Inspiring
November 26, 2016
Answered

Call a class function from a timeline

  • November 26, 2016
  • 2 replies
  • 2042 views

I have a sprite named Jim, and Jim has a mouth named Jaw.

Jim has a function I want to call once he closes his mouth.

This function works perfectly when called from Jim's timeline, but when called from the jaw's timeline, it goes wrong.

Suppose the function is named "function()", "function()" doesn't work, nor does "Jim.function".

What do I do? I'm really starting to hate working with classes.

    This topic has been closed for replies.
    Correct answer Colin Holgate

    What you describe doesn't seem to involve classes, but in any case, if it's AS3 try this:

    MovieClip(parent).function();

    from within 'jaw'. If it's HTML5, try:

    this.parent.function();

    2 replies

    kglad
    Community Expert
    Community Expert
    November 26, 2016

    if you need to call function (eg, jawF in class Jim) from outside Jim, you need to use a non-private function in Jim.

    eg, in Jim:

    public function jawF():void{

    }

    and in a class that has a reference to a Jim member, eg

    var jim:Jim = new Jim();

    you can use

    jim.jawF();

    Colin Holgate
    Colin HolgateCorrect answer
    Inspiring
    November 26, 2016

    What you describe doesn't seem to involve classes, but in any case, if it's AS3 try this:

    MovieClip(parent).function();

    from within 'jaw'. If it's HTML5, try:

    this.parent.function();

    Jon Chambers
    Inspiring
    November 26, 2016

    The addition of "MovieClip(parent)" made everything work exactly as I expected. That said, I have only a vague idea of what "MovieClip(parent)" probably means, and don't know if there is any other "...(parent)" or "MovieClip..." I have to learn.

    Could you please refer me to a tutorial that will fill this knowledge gap of mine?

    Colin Holgate
    Inspiring
    November 26, 2016

    Don't know of a tutorial, but I can explain this bit easily enough.

    In ActionScript the compiler needs to be convinced that what you're trying to do is legal. The thing that 'jaw' is inside need not be a MovieClip, there are various other display objects that are capable of containing things, and some of those are not able to include code. You know that it's a MovieClip, but for the compiler to be sure that it is you spell it out, by saying MovieClip(parent).function() instead of just parent.function().

    In programming that would be called type casting, where you force the compiler to think of the variable as being a particular type.