Skip to main content
AskingAboutJS
Inspiring
January 16, 2017
Answered

Captivate 9 OnLoad Not Working

  • January 16, 2017
  • 1 reply
  • 1291 views

I cannot figure out why, but my On Enter event is not working as it should.

I have a captivate variable called "level", which is set to a default value of "0".

On every slide(level), I have an On Enter event (javascript).

The On Enter javascript uses this code (for level 1a):

window.cpAPIInterface.setVariableValue("level", "1a");

Issue:

Regardless of which level I complete (e.g. 1b, 2a, 2b, 3a, etc), the variable remains with a value of "1a" (the first level).

Note: On Enter should for each slide(level) set the "level" variable to its respective value (not 1a).

I have tried using the EventListener TWO ways in each slide(level)'s On Enter event, but all that did was always keep the variable set to the default "0".

Here is the code (for level 1a) WAY ONE:

var levelSetup = function(){

    window.cpAPIInterface.setVariableValue("level", "1a");

}

window.addEventListener('moduleReadyEvent', levelSetup);

Here is the code (for level 1a) WAY TWO:

window.addEventListener('moduleReadyEvent', levelSetup); // I TRIED THIS HERE AND BELOW THE FUNCTION

function levelSetup() {

    window.cpAPIInterface.setVariableValue("level", "2b");

}

This topic has been closed for replies.
Correct answer TLCMediaDesign

I have removed all other code temporarily and only left that one line of code for changing the level variable's value. It doesn't work. It would look like the above post. p.s. The other code also wasn't working.

I have working javascript in the smartshapes on the slides. It's just the On Enter javascripts that don't work.


Is this swf or HTML5?

I would suggest putting the JavaScript in the HTML page. You can edit the JS and refresh in the browser instead of republishing the project. I have posted many examples of adding the event listeners in the page.

This is the code to add the event listener in the HTML:

var interfaceObj, eventEmitterObj;

window.addEventListener( 'moduleReadyEvent', function ( e )
{
interfaceObj = e.Data;
eventEmitterObj = interfaceObj.getEventEmitter();
initializeEventListeners();
});

function initializeEventListeners()
{
if ( interfaceObj )
{
     if ( eventEmitterObj )
  {
         eventEmitterObj.addEventListener( 'CPAPI_SLIDEENTER', function ( e )
   { 
    //execute code here   
            });
  }
}

1 reply

TLCMediaDesign
Inspiring
January 16, 2017

Your variable "level" is always going to be "1a" because you are setting it on enter of every slide. Because this script is working correctly:

window.cpAPIInterface.setVariableValue("level", "1a");

The module ready event only fires once in the project and most likely has already fired when the script executes.

Not sure what you are trying to do, in what circumstance would the variable change from "1a" to "2b"?

Your best bet is to use an external JavaScript and use the slide enter event listener.

AskingAboutJS
Inspiring
January 17, 2017

I see. That explains the "0" when I use EventListener.


However, that setvariable line of code above is different on each slide: e.g.

Slide1: window.cpAPIInterface.setVariableValue("level", "1a");

Slide2: window.cpAPIInterface.setVariableValue("level", "1b");
Slide3: window.cpAPIInterface.setVariableValue("level", "2a");

Slide4: window.cpAPIInterface.setVariableValue("level", "2b");
and so on...

The weird thing is... I have multiple On Enter javascripts which work in this project (slide1 and hiscores)... but slide2, 3, 4, etc never activate :s

TLCMediaDesign
Inspiring
January 17, 2017

In your on enter action for slide "2a" is that the only script you are executing?

Can you post what is in the JavaScript window?