How to move an object with arrows

Hello, I've just started p5 programming and I'm really into it , but I can't figure out how to move an object with direction arrows. function setup(){ createCanvas(1250,1000); bug = new obj(); } function draw(){ background(50,89,100); bug.display(); stroke(255); if(keycode == LEFT_ARROW) bug.move(-this.speed,0); if(keycode == RIGHT_ARROW) bug.move(this.speed,0); if(keycode == UP_ARROW) bug.move(0,-this.speed); if(keycode == DOWN_ARROW) bug.move(0,this.speed); }

`function obj(){
    this.y=random(height);
    this.x=random(width);
    this.diameter=random(10,30);
    this.speed=10;
`

`this.move = function(posx,posy) {
        this.x+=posx;
        this.y+=posy;
  };
`

    `this.display = function(){
        ellipse(this.x,this.y,this.diameter,this.diameter);
    }
};

`

That's my code , but my object seems to be stuck in the canvas , not moving at all.

Tagged:

Answers

Sign In or Register to comment.