I am quite new to Hype and my last experience with Javascript is a while ago as well.
I am trying to add the current running time to many of my scenes. I’ve already tried many things including setInterval or setTimeout. But nothing seems to work. I also tried to add the code in an innerHTML element. But this didn’t work either.
Further I’d like to know how it is possible to use one function in many scenes when you’re not allowed to use the same id in different scenes.
Thanks in advance. I might have a silly error in reasoning but I just can’t figure it out.
The following code is from A Book About Hype... A Book About Hype ...in the "Timelines" section of the "JavaScript" chapter.
var seconds = hypeDocument.currentTimeInTimelineNamed("Main Timeline");
hypeDocument.getElementById("text").innerHTML = seconds.toFixed(2);
Basically, that uses the Hype JavaScript API to get the current time in a timeline. In this example, it's the "Main Timeline". That value is then displayed to a text element, with the unique ID of "Text". Lots of decimal places are used, so the variable "seconds" is shortened to two decimal places.
You have to set an interval to fire every second (if you want it live) make sure to clear the interval on scene unload so it doesn’t effect performance (maybe)
• There is a persistent symbol that is viewable on all scenes and has a function “running” on symbol load that creates a new Date() and returns the hours, minutes and seconds.
• Below is a copy of the document as a template and below that there is the symbol itself which you can import into your documents (using the Symbol->Import Symbol… option in the menu)
For it to function over every scene, using a persistent symbol is the easiest.
If you use a standard symbol then you would have to add a different symbol with different ID's for the text element each time. (all elements must have a unique ID)
Because persistent symbols live outside the scene environment you can add them to many scenes (but only 1 per scene you cannot have the same one duplicated in a scene)
So, to summerize, use
• persistent symbols when you want the same thing to appear across many scenes (like a menu)
• standard symbols when you want the same thing to appear throughout a scene