Counting with JavaScript

I'm wondering how to recreate the counting function that is native within Storyline in Javascript.

For example:

In Storyline, you can create a variable and then use the +Add operator to count the number of times a user clicks a button:

Action: Adjust variable
Variable: noOfClicks
Operator: + Add
Value: Value   1
When: User Clicks
Object: Button

In Javascript, you could do something like the following but there is something missing. With the code below, it will set the "jsClicks" variable to 1 but subsequent clicks do not increment the count up.

var player = GetPlayer( );
var noOfClicks = 0;
noOfClicks ++;
player.SetVar("jsClicks",noOfClicks);

What would I need to do to increment it up each time the user clicks the Button object?  

Thanks for the help.

 

6 Replies
OWEN HOLT

That being said... try something like this.

I used a SL numeric variable I called "ClickCount" with a default value of "0". The following code worked to increase the count incrementally by one each time the user clicked the button that the code was attached to.

var player=GetPlayer();
var clicks = player.GetVar("ClickCount") +1;
player.SetVar("ClickCount",clicks);

Richard Watson

Owen,

That accomplishes what I was playing around with. Thank you.

In regard to your earlier question, "If it can be done in the player, why not use the player..." 

I realize I can do this in the player but I have multiple other things going on in an interaction that I'm playing with and I wanted to see how much of it I could incorporate into the actual javascript itself.  Call it an experiment if you will; just learning what I can and cannot do with it at the moment.

Thanks again for the sample.

Richard