0

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)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jan2000
  • 3
  • 5

1 Answers1

0

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.

James Hibbard
  • 16,490
  • 14
  • 62
  • 74
  • Thank you for your answer! But this doesn't work inside Construct 2, as I am only able to controll what happens after the – jan2000 May 12 '14 at 18:35
  • Then just move everything into the inline event handler: `` – James Hibbard May 12 '14 at 18:55
  • I just have an "execute javascript"-option in Construct 2. And this is what I put in it (but it doesn't work): "window.plugins.headsetdetection.detect(function(detected){if(!detected){ alert('No headphone detected'); // Set your variables here }})" – jan2000 May 12 '14 at 19:11
  • Note: Other javascript execute commands (from Phonegap) works, like f.ex: "navigator.notification.vibrate(2500);" – jan2000 May 12 '14 at 19:15
  • is there an error in the syntax? Because this command gives me a dialogue: "window.plugins.headsetdetection.detect(function(detected) {alert(detected)})" – jan2000 May 12 '14 at 19:31
  • You need to remove the comment, this is causing the rest of the line to be ignored: `window.plugins.headsetdetection.detect(function(detected){if(!detected){ alert('No headphone detected');}});` – James Hibbard May 12 '14 at 19:31
  • Good stuff. Glad we got there in the end :) – James Hibbard May 12 '14 at 20:03