0

我正在“ Construct 2 ”中构建一个 iOS 游戏,并尝试使用 Phonegap 插件“耳机检测”来提醒是否没有插入耳机。

我可以创建一个警报框(true/falls)来判断用户是否通过以下方式插入耳机:

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

但我是 javascript 新手,想知道:

我如何能:

  • 仅在检测为 FALSE 时发出警报?或者(甚至更好):
  • 让上面的布尔变量在 Construct 2 中设置另一个变量?(这样我可以在我的游戏设计中建立其他依赖关系)
4

1 回答 1

0

您应该使用不显眼的事件侦听器,而不是内联 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);

然后您可以检查检测到的错误值:if(!detected){并在回调中设置您需要的任何变量。这些变量需要以它们此时在范围内的方式进行定义。

于 2014-05-12T17:55:02.587 回答