Question regarding KeyCode & if/else statements

Hello,

I am new to processing and currently I am only testing random staffs. I tried to create a simple circle which is moved by 50 to the UP and DOWN depending on which button on the keyboard you pressed. I am using a simple if/else method. The problem that occurred is the fact that for some reason when you press UP (for example) instead of moving just by 50 it keeps going upwards.

I tried to fix the problem by my own but my knowledge wasn't enough. If someone can help me and explain where my mistake is I will be really grateful.

The code that I wrote:

Main.js:

var Circle;

function setup(){
    createCanvas(600,600);
    Circle = new Circle();
}

function draw(){
    background(65, 35, 255);
    Circle.move();
    Circle.show();
}

Circle.js

function Circle(){
    this.x = 300;
    this.y = 300;

    this.xup=50;
    this.yup = 50 ;

    this.move = function(){
       if (keyCode === UP_ARROW){
          this.y = this.y - this.yup;
       }
       else if (keyCode === DOWN_ARROW){
          this.y = this.y + this.yup;
       }
    }

    this.show = function (){
       fill(74, 124, 35);
       ellipse(this.x, this.y, 100, 100);
    }
}

Tagged:
Sign In or Register to comment.