Question

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:

  • Only alert if the detection is FALSE? OR (even better):
  • Let the above boolean variable set another variable inside Construct 2? (That way I can make other dependancies in my game design)
Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top