Playing a timeline with Conditional Javascript


(Tamara Cutler) #1

I am new to code and am trying my best but can someone tell me what I might be doing wrong here? I have created a quiz that accumulates points and displays the total in the text field with unique ID “score” on the last scene.
I then want a timeline to play based on what the score value is. Timelines are called:
scoreA, scoreB, scoreC or scoreD
I made a function called playscore but I am still doing something wrong as it is not quite working. Any help would be appreciated. Hype file is also attached.
quizdemo3.hype.zip (71.4 KB)

function playScore(hypeDocument, element, event){

if hypeDocument.getElementById(‘score’) >= 8
hypeDocument.startTimelineNamed(‘scoreA’, hypeDocument.kDirectionForward);
if hypeDocument.getElementById(‘score’) >= 6
hypeDocument.startTimelineNamed(‘scoreB’, hypeDocument.kDirectionForward);
if hypeDocument.getElementById(‘score’) >= 4
hypeDocument.startTimelineNamed(‘scoreC’, hypeDocument.kDirectionForward);
if hypeDocument.getElementById(‘score’) >= 2
hypeDocument.startTimelineNamed(‘scoreD’, hypeDocument.kDirectionForward);
}

I will at some point need to figure out how to reset the timelines as well.


(Mark Hunte) #3

A couple of things.

1,

hypeDocument.getElementById(‘score’)
Gest the element.
i.e the object on the page that in this case is displaying the score. It does not get the displayed text.

Even if you do dive deeper and get the innHTML or innerTxt you would need some more code to parse it to a literal number or your conditions would need to look for a string value.

2,
you already have the score in a global var ‘window.quizScore’

So you do not need to get the value from the score element any way.

3,

rather than use an if conditional use a switch.
Which you can use your global var as the expression to match against.

switch( window.quizScore  ) {
case  8 :
hypeDocument.startTimelineNamed('scoreA', hypeDocument.kDirectionForward);
 break;
case  6 :
hypeDocument.startTimelineNamed('scoreB', hypeDocument.kDirectionForward);
 break;
case  4 :
hypeDocument.startTimelineNamed('scoreC', hypeDocument.kDirectionForward)
 break;
case  2 :
hypeDocument.startTimelineNamed('scoreD', hypeDocument.kDirectionForward); 
}

(Tamara Cutler) #4

Thanks Mark, I will try to absorb this.