Jump to content
gearmobile

Method onComplete - few functions

Recommended Posts

I am new to the study of GSAP. In the study a ready example of a HTML5-game ( GSAP ) I have a question.

 

The method onComplete is passed to one callback function:



...
onComplete: oneFunction


 

Is it possible onComplete method to pass multiple callback functions?

 



...
onComplete: firstFunction secondFunction


 

For example, I have a task to hang on one event call multiple functions.

 

Or is this way impossible and the only solution in this case is to call the callback functions in the chain - at the conclusion of one function this function calls another function and so on?

Share this post


Link to post
Share on other sites

There can only be 1 function per callback type. What you're describing would be an event emitter/dispatcher. Using an event emitter is a common approach to game design, and it's something I wondered about when I first started using GSAP because I noticed in the source code that GSAP uses an event emitter to drive the callbacks.

 

There are ways to tap into GSAP's event emitter or intercept the callbacks, but that's not something I would recommend doing. If you want to use events, you could set a callback to emit an event from your own emitter. Here's a demo I made using that approach. Instead of running the logic to figure when a leaf should grow in a callback, I just emit an event and let the leaves figure that out. When a leaf starts growing, it stops listening for that event.

 

See the Pen gpGEye?editors=0010 by osublake (@osublake) on CodePen

 

That demo might be a little hard to understand, so here's a really simple example using EventEmitter3. You will see listenerA gets called when the tween first starts, listenerB gets called on the first repeat, and listenerC gets called 2 times on every repeat.

 

See the Pen 4bc0ee5b3e3f3e41647140e836619439?editors=0011 by osublake (@osublake) on CodePen

 

.

  • Like 4

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.