A bit of background – I had been a Flash developer for years, and had been out of the business for close to a decade. I have since returned, and as an re-introduction to programming I built a gaming engine from scratch in javascript for a series of interactive art pieces.
The agency I am working with now has a number of developers working in Hype, and they have brought me on to help them integrate more coding into their projects. It took a while, but I have begun to grasp all the elements of Hype, and I have wanted to build a series of best practices for the team I am working with.
I am enjoying the multiple layouts, but a concern of mine has been the duplication of IDs. What I like about Hype is that it by default generates a unique ID for each symbol instance on the Stage(scene). I wanted to come up a way that if the responsive version on Mobile was different due to dimensions and stacking, that we could keep much of the same code, and symbols and plug in the ones that have changed for the layout. To do that I have built a global object I am calling ALL_ELEMENTS which has nested objects, MOBILE, TABLET_H, TABLET_V and DESKTOP.
On the initialization of the Stage, all the elements that are symbols are gathered and the automatically generated IDs have a corresponding name that we can use in the code that the developer can read, not the randomly generate ID.
In the DOM, all of the Symbols in Hype get nested in a container element with a class, HYPE_element. I had figured out a bit of a clunky algorithm to step through the DOM with JQuery to get the nested symbols, but there was no way to know generically what symbols were, or more specifically their name, just by reading the DOM.
Then it hit me, I can use classes to get all the information that I need across and then it becomes quite easy to build out.
On every Symbol onSymbolLoad I have a JS function
function symbolRegistry(hypeDocument, element, event){
var symbolName = hypeDocument.getSymbolInstanceById(element.id).symbolName();
$(element).addClass(“symbol”);//for catching all symbols
$(element).addClass(“symbolName-” + symbolName);//symbol name from library
}
So for instance I now have a child that generates a div that looks like this:
<div class="HYPE_element symbol symbolName-Level3_Child"When implementing this on the main function for each layout, I can just call:
var level3SymbolInstances = $(".symbolName-Level3_Child");
I get an array of all the instances of this specific symbol, in the case of my demo, 4.
What is interesting about Hype and how it builds out the DOM, it uses the nesting of symbols as containers for subsequent divs, but it also uses the layer order for placement. It makes sense but it proved a revelation for reading the elements.
The developer can look at the symbols and read them to easily figure out which generic id corresponds to the instance on the stage, so that then unique references in the ALL_ELEMENTS can be given. The instances get register to the appropriate sub object in ALL_ELEMENTS but a global g_LAYOUT that gets set on initialization.
In my working example, I can make drastically different looking versions for responsiveness but still act on them with the same group of functions, because the names of the symbols are the same for the code even though the IDs are different.
This is important because when the Hype doc gets compiled it saves every layout in a different parent div.
I have also build another global object called TIMELINE_PLAYHEADS, that allows the developer to register a timeline , start and duration in frame count, apply frameNames for keyframes in a specific timeline so they can target specific frames on a timeline easily, and go to any frame on a timeline, by using the frame number and not timecode.
My thinking at this point is to build experiences as a series of nested symbols, and then use the Timelines like frame names in a MovieClip.
There is more functionality I am looking to integrate, and as it gets closer to the model I want to work with, I will upload a version.