Open TravelTrader opened 11 months ago
I think that what you describe should work... How are you testing these changes?
Thank you for your fast reply.
Here’s the complete file controls.js
with both approaches:
class Controls {
constructor(cv) {
this.forward = false;
this.left = false;
this.right = false;
this.reverse = false;
this.canvas = cv
this.#addKeyboardListeners;
}
#addKeyboardListeners() {
document.onkeydown = (event) => {
switch (event.key) {
case 'ArrowLeft':
this.left = true;
break;
case 'ArrowRight':
this.right = true;
break;
case 'ArrowUp':
this.forward = true;
break;
case 'ArrowDown':
this.reverse = true;
break;
}
}
document.onkeyup = (event) => {
switch (event.key) {
case 'ArrowLeft':
this.left = false;
break;
case 'ArrowRight':
this.right = false;
break;
case 'ArrowUp':
this.forward = false;
break;
case 'ArrowDown':
this.reverse = false;
break;
}
}
this.canvas.ontouchdown = (event) => {
console.log(event);
this.forward = true;
}
documemt.ontouchdown = (event) => {
console.log(event);
this.forward = true;
}
}
}
Looks like you're not calling the private method: Change: this.#addKeyboardListeners; To: this.#addKeyboardListeners();
Changed that (and my misspelled „document“) but no touch is recognized.
Update: Tested on laptop and keys also not working. I’m sorry, I’ll check for mistakes in all files myself and let you know the result.
So, with the additional brackets, it works with keys on the computer.
Also, when adding onmousedown
with a mouse event.
But with the touch function in the laptop browser there’s also no reaction…
It’s possible to ignore the touch events and solve it with mouse events that are working on mobile but for steering controls it would be much better to get multiple touches to drive and steer at the same time like it’s done with the keys. So, it’s not urgent but I’d be happy to know how to do it.
I want to add mobile controls by touch and I tried to add
document.ontouchstart = (event) => {}
and alsothis.canvas. ontouchstart = (event) => {}
(by adding the canvas object to Car and then to Controls). But both didn’t work. Checked that by puttingconsole.log(event);
and/orthis.forward = true;
inside the above functions.Could anyone help me to get that work and especially learn what I did wrong?
Thank you.