如何仅在布尔值为false时发出警报(与/或在构造2中设置javascript变量)
How to only alert when boolean is false (AND/OR set variable with javascript in Construct 2)
我正在"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){
,并在回调中设置所需的任何变量。这些变量需要以一种在此时它们处于作用域中的方式来定义。
相关文章:
- 如何在定义js文件后为外部javascript文件设置变量
- 在 Jquery/Javascript 中使用多个 OR (||) 运算符时如何设置变量
- 在值内设置变量
- 在javascript函数中设置变量
- 设置变量时破坏游戏代码
- 未设置变量的赋值| jQuery的作用是什么
- 在可编辑行内设置变量
- 在不中断形式帖子的情况下在角度中设置变量
- MongoDB MonkAPI根据数据库请求之外可用的查找结果设置变量
- text方法中的javascript设置变量
- 如何为JavaScript函数设置变量以嵌入视频
- 使用jQuery和PHP加载区域设置变量
- 包括外部JS文件和设置变量
- 在jQuery File upload插件中设置变量上传路径
- 设置变量函数不起作用
- Session.get未在Meteor.js中设置变量
- 为cytoscape.js mapData设置变量限制
- 如何在脚本标记中设置变量
- 使用控制器在conditional.js.erb文件中设置变量
- 如何从控制台动态设置变量