I want to add a key to my Javascript code on P5.js

Asked

Viewed 182 times

2

I have these two functions, however, my problem is in moveRacketE(), I’m doing a ping pong, only I wanted to make for two people to play, and not I play against the computer, as I could add the W key to go up, and the S key to go down in moveRacketE function()?

function moveRacket() {
  if(keyIsDown(UP_ARROW)) {
     yRacket -= 10;
  }
  if(keyIsDown(DOWN_ARROW)) {
    yRacket += 10;
  }
}

function moveRacketE() {
  if(keyIsDown(event.key == 87)) {
     yRacket -= 10;
  }
  if(keyIsDown(event.key == 83)) {
    yRacket += 10;
  }
  • 1

    Try to use if(keyIsDown(87)) and if(keyIsDown(83))

  • I used, however, it is moving my racket, and I would like to move the enemy racket

  • I managed to tidy up, I had to change the yRacket to yRacketE, which was the variable I created, thank you very much.

1 answer

2


Reading the documentation of P5, the problem is that you are sending a boolean, comparing event.key == 87, when in fact you just have to compare the direct value using the P5 keycodes.

Try replacing your code with the following:

function moveRacketE() {
    if(keyIsDown(87)) { //W
       yRacket -= 10;
    }
    if(keyIsDown(83)) { //S
       yRacket += 10;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.