Jump to content
GreenSock

njvan

Hittest only working on the first instance

Moderator Tag
Go to solution Solved by OSUblake,

Recommended Posts

Hi again, in my codepen example - hittest works as expected but only in the first instance. What would be the recommended way to detect a collision with multiple dynamically created elements?

See the Pen eYeKypy by njvan (@njvan) on CodePen

Link to comment
Share on other sites

  • Solution

Hi njvan,

 

Calling hitTest like you are doing will only call it on the first .pad element and the first .note element. You would need to loop through each pad and each note to do it like you want. That's going to be a little wasteful, so it would be best to only check notes that are in the same track as the pad. To further optimize, you should only check notes that are active. Notice how I added an active class to the element right before it animates and remove it right after it animates.

 

If you need any help understanding what I did here, just let me know.

 

See the Pen wvPxwRE by GreenSock (@GreenSock) on CodePen

 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

That's amazing! Adding the active class was genius. I had started down a rabbit hole of trying to get the currently animating element in the timeline. This is much simpler. Thank you so much, @OSUblake!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×