Skip to main content
Participating Frequently
April 10, 2018
Answered

Is there an event for frame change?

  • April 10, 2018
  • 1 reply
  • 583 views

I am building a menu using HTML5 canvas.

The menu has tons of submenus, each of which has a bunch of buttons.

Each button gets an event listener added to it when that frame loads.

Every time the user hits a button, the main timeline skips to a different frame (a different submenu).

From what I've read, I need to remove the event listeners for the buttons that are no longer visible when this happens.

That means most of my buttons need to have code that removes all event listeners, and only then changes frames.

This is bad, since I might forget to add this code to one of my buttons at some point, and the next time I come back to that menu, it'll add a second event listener for the button, making the function that handles the button click fire twice.

Is it possible to add an event listener that triggers when the frame changes? If so, I could just clear all event listeners that I setup on any buttons anywhere every time the frame changes, and not have to do it within each button's code.

    This topic has been closed for replies.
    Correct answer ClayUUID

    Having button event handlers directly twiddle the current timeline frame is a fairly terrible design. It would be far better to have your button handlers instead call a common function, passing the desired frame as data. Then the common function could handle any necessary cleanup of the current menu before gallivanting off to the next one.

    1 reply

    Community Expert
    April 10, 2018

    hi,

    why not just place your entire menu in a MovieClip, and have it on top of your entire project? So it will always be the same menu.... and it will just navigate or load other MovieClips. Then instead of your submenus skipping to a different frame on main timeline, its just moving to a different frame (or I would use Labels) within the main timeline of the MC?

    So you could just use a MovieClip for each of your "pages". So think of a MovieClip as a new scene, or even like a new file.... then your entire project can be ONE frame... with a Menu in one MC... and then one Main MC on the main timeline that swaps out the various MC's depending on which menu item is clicked... or the menu can be the same, and load a new URL that has your other sections.

    You can swap MCs from the library

    hope that helps.

    cheers,

    mark

    headTrix, Inc. | Adobe Certified Training & Consulting
    Participating Frequently
    April 10, 2018

    That sounds similar to how I do things now, except everything is not contained within a single movie clip.

    I have a main timeline, and frame labels for all my frames:

    Each frame is a different submenu. When a button is hit on any of these frames, it uses gotoAndStop(LABEL) to move the main timeline to a specific submenu.

    It sounds like you are proposing I would do exactly that, but inside of a single movie clip.

    Then all my submenus are their own movie clips, on their own frames, and I skip between frames like I do now.

    Is there some advantage to doing that? Does that help with event listeners somehow?

    ClayUUIDCorrect answer
    Brainiac
    April 10, 2018

    Having button event handlers directly twiddle the current timeline frame is a fairly terrible design. It would be far better to have your button handlers instead call a common function, passing the desired frame as data. Then the common function could handle any necessary cleanup of the current menu before gallivanting off to the next one.