Adding running time to many scenes


(Anica) #1

Hi there,

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.


#2

The following code is from A Book About Hype… https://photics.com/hype-book …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.


(Anica) #3

The phrase “running time” might have been confusing.
I mean the actual time (e.g. now 11:41)

It works that it shows the current time when loading the scene but it doesn’t change. So the time isn’t “running”.


#4

OK. now that that’s clear :slight_smile:

One of the ways to have the same thing over multiple scenes is to use a persistent symbol

Within this symbol you can create a javascript function to run on symbol load that updates the time


(Anica) #5

Yes, I tried it with using a symbol but it doesn’t work either.

And still the time isn’t updating. It loads the current time once when the scene is loaded and that’s it.


#6

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)

Would you like me to show you?


(Anica) #7

Yes please!


#8

• 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)

runningTime.zip (38.3 KB)

runningTime.hypesymbol.zip (38.0 KB)


(Anica) #9

Thank You!
I’m gonna test it right away.

But does it have to be a persistent symbol? And if, why?
I already got a usual symbol on each scene. But I can’t use those, can I?


(Anica) #10

I changed every symbol to a persistent symbol. Now it’s working!


#11

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