-1

I built centipede in javascript from a Khan Academy tutorial. Then I figured out how to put it into a web browser. however, the key presses are not working. I have tried to change the keyCode values, and change some of the function definitions to "void" but nothing has worked. The app uses processing .js to work. Here is the section of js dealing with the keys:

var Player = function(x,y,size,speed){
    this.x = x;
    this.y = y;
    this.size = size;
    this.speed = speed;

    this.update = function(){
        if(keys[LEFT]){
        this.x -= this.speed;
        }
        if(keys[RIGHT]){
        this.x += this.speed;
        } 
        if(keys[DOWN]){
        this.y += this.speed;
        }
        if(keys[UP]){
        this.y -= this.speed;
        }
        if(keys[76] && bulletDelay < 0){
        var b = new Bullet(this.x, this.y, 5,5);
        bullets.push(b);
        bulletDelay = 40;
        }


       if (this.x < 0){
         this.x = 0;
        }
       if (this.x > width){
         this.x = width;
       }
       if (this.y > 800){
         this.y = 800;
       }

   //This adjusts the max height the player can move up on y-axis. Adjust to make more like Atari version
   if (this.y < 100) {
    this.y = 100;
   }

   noStroke();
   fill(0,255,0);
    ellipse(this.x, this.y, this.size, this.size);
};
};
Eugene
  • 10,957
  • 20
  • 69
  • 97
bamabacho
  • 191
  • 2
  • 16

2 Answers2

-1

The syntax

if(keys[LEFT])

should be

if(keyCode == LEFT)

See the Processing.js keyCode reference and key

EDIT 1: For special keys (arrow, space), you should use keyCode instead of key.

Eugene
  • 10,957
  • 20
  • 69
  • 97
-1

I sorted it out. The keyPress and keyRelease functions needed to be changed from

var keyPressed = function(){

to

void keyPressed() . . .

similar for the keyReleased function

bamabacho
  • 191
  • 2
  • 16