javascriptcordovavariablesconstruct-2

How to only alert when boolean is false (AND/OR set variable with javascript in Construct 2)


I am building an iOS-game in "Construct 2" and trying to use the Phonegap-plugin "Headset detection" to alert if a Headset is not plugged in.

I am able to create an alert-box (true/falls) to tell if the user has plugged in a headset via this:

<button onclick="window.plugins.headsetdetection.detect(function(detected) {alert(detected)})">headphone detected?</button>

But I am new to javascript, and would like to know:

How do I:


Solution

  • You should use unobtrusive event listeners, as opposed to inline JS:

    <button id="headphone">headphone detected?</button>
    
    function detectHeadphones(){
      window.plugins.headsetdetection.detect(function(detected){
        if(!detected){
          //No headphone detected
          alert("No headphone detected");
    
          // Set your variables here
        }
      })
    };
    
    var headphoneButton = document.getElementById("headphoneButton");
    headphoneButton.addEventListener('click', detectHeadphones);
    

    Then you can check detected for a falsey value thus: if(!detected){ and set whichever variables you need within the callback. These variables will need to have been defined in a way that they are in scope at this point.