如何仅在布尔值为false时发出警报(与/或在构造2中设置javascript变量)

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

本文关键字:设置 变量 javascript false 布尔值 何仅      更新时间:2023-09-26

我正在"Construct 2"中构建一个ios游戏,并试图使用phonegap插件"耳机检测"来警报,如果耳机未插入。

我可以创建一个警报框(true/falls)来告诉用户是否已经插入耳机:

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

但我是新的javascript,并想知道:

如何:

  • 仅在检测为FALSE时发出警报?或者(更好):
  • 让上述布尔变量在Construct 2内设置另一个变量?(这样我就可以在游戏设计中添加其他依赖项)

你应该使用不显眼的事件监听器,而不是内联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){,并在回调中设置所需的任何变量。这些变量需要以一种在此时它们处于作用域中的方式来定义。