Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 1: Adding touch to the Controls class? #8

Open
TravelTrader opened this issue Jan 3, 2024 · 6 comments
Open

Lesson 1: Adding touch to the Controls class? #8

TravelTrader opened this issue Jan 3, 2024 · 6 comments

Comments

@TravelTrader
Copy link

I want to add mobile controls by touch and I tried to add document.ontouchstart = (event) => {} and also this.canvas. ontouchstart = (event) => {} (by adding the canvas object to Car and then to Controls). But both didn’t work. Checked that by putting console.log(event); and/or this.forward = true; inside the above functions.

Could anyone help me to get that work and especially learn what I did wrong?

Thank you.

And if you, Radu, will read it: The courses are really great. Calm, in detail, perfect. Thanks.

@gniziemazity
Copy link
Owner

I think that what you describe should work... How are you testing these changes?

@TravelTrader
Copy link
Author

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;
    }
  }
}

@gniziemazity
Copy link
Owner

Looks like you're not calling the private method:
Change: this.#addKeyboardListeners;
To: this.#addKeyboardListeners();

@TravelTrader
Copy link
Author

TravelTrader commented Jan 3, 2024

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.

@TravelTrader
Copy link
Author

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…

@TravelTrader
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants