在你的代码里面加入 cocotouch.js
文件,如果是不支持的浏览器里的话,会 Bluetooth API 不兼容的错误,我们推荐你使用最新的 Chrome 浏览器。
<script src="./cocotouch.js"></script>
const cocotouch = CocoTouch();
const cocotouch = CocoTouch();
const connectBtn = document.querySelectorAll('.connect-btn');
$(connectBtn).on('click', function(evt) {
// 调用 connect 方法尝试连接,此时浏览器会弹出蓝牙配对对话框
cocotouch.connect();
return false;
});
其次可以来了解几个事件:
成功连接 CocoTouch 时触发。
const connectBtn = document.querySelectorAll('.connect-btn');
cocotouch.on('connected', () => {
$(connectBtn).addClass('hide');
});
CocoTouch 连接断开时触发。
cocotouch.on('disconnected', () => {
$(connectBtn).removeClass('hide');
});
当 CocoTouch 接收到消息的时候触发。
cocotouch.on('message', (message) => {
if (!message) return;
if (message == '0') {
moveLeft(); // 当触碰消息为 0 的时候,左移动
} else if (message == '1') {
moveTop(); // 当触碰消息为 1 的时候,上移动
} ...
// 其他任意指定
});
npm install http-server -g;
hs;
open http://localhost:8080/examples/pacman/index.html // 从默认端口打开游戏页面