this.cursors = this.input.keyboard.createCursorKeys();
// …
if (this.cursors.right.isDown) {
// move player right
}Unconventional controls / notebook
Unconventional controls
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
window.addEventListener("keydown", (event) => {
console.log(event.keyCode);
});window.addEventListener("keydown", (event) => {
switch (event.keyCode) {
case 8: {
// Pause the game
break;
}
case 588: {
// Detonate bomb
break;
}
// …
}
});const toDegrees = 1 / (Math.PI / 180);
let horizontalDegree = 0;
let verticalDegree = 0;
const degreeThreshold = 30;
let grabStrength = 0;Leap.loop({
hand(hand) {
horizontalDegree = Math.round(hand.roll() * toDegrees);
verticalDegree = Math.round(hand.pitch() * toDegrees);
grabStrength = hand.grabStrength;
output.innerText = `Leap Motion:
roll: ${horizontalDegree}°
pitch: ${verticalDegree}°
strength: ${grabStrength}
`;
},
});function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// …
if (horizontalDegree > degreeThreshold) {
playerX -= 5;
} else if (horizontalDegree < -degreeThreshold) {
playerX += 5;
}
if (verticalDegree > degreeThreshold) {
playerY += 5;
} else if (verticalDegree < -degreeThreshold) {
playerY -= 5;
}
if (grabStrength === 1) {
alert("BOOM!");
}
ctx.drawImage(img, playerX, playerY);
requestAnimationFrame(draw);
}doppler.init((bandwidth) => {
const diff = bandwidth.left - bandwidth.right;
});const Cylon = require("cylon");
Cylon.robot({
connections: {
arduino: { adaptor: "firmata", port: "/dev/ttyACM0" },
},
devices: {
makey: { driver: "makey-button", pin: 2 },
},
work(my) {
my.makey.on("push", () => {
console.log("Button pushed!");
});
},
}).start();