key.code = 37和key.code = 39不起作用

时间:2019-09-18 15:34:09

标签: javascript processing.js

我正在尝试根据用户按下的箭头(左或右)左右移动佳能。这是我的代码:

size(400, 400, P2D);

//Constructor canon
var Canon = function(canonX, canonY, W){
fill(0, 0, 0);

    this.canonX = canonX;
    this.canonY = canonY;
    this.W = W;
};

//CANON MOVELEFT
Canon.prototype.moveLeft = function(){
    this.canonX -= 5;
};
Canon.prototype.moveRight = function(){
    this.canonX += 5;
};

//DRAW CANON
Canon.prototype.drawCanon = function(){
    quad(this.canonX - this.W/5, this.canonY + this.W * 2, this.canonX + 4/5 * this.W, 
this.canonY + 2 * this.W, this.canonX + 3 * this.W/5, this.canonY, this.canonX, this.canonY);
    ellipse(this.canonX + this.W/10 * 3, this.canonY + 2 * this.W, this.W, this.W/ 5 * 3);
    //wheels
    fill(138, 109, 23);
    ellipse(this.canonX - this.W/50 * 12, this.canonY + this.W/50 * 102, this.W/5 * 2, this.W/5 * 6);
    ellipse(this.canonX + this.W/50 * 42, this.canonY + this.W/50 * 102, this.W/5 * 2, this.W/5 * 6);

};

var canon = new Canon(199, 200, 50);
draw = function() {
    background(255, 255, 255);
    if(keyIsPressed && key.code === 37){
        canon.moveLeft();
    }
    else if(keyIsPressed && key.code === 39){
        canon.moveRight();
    }
    canon.drawCanon();

};

对于两个函数moveLeft()和moveRight(),我都尝试使用32的e keyCode(用于空格)。但是不能使用左右箭头的keyCode ... 有人可以帮我吗?

0 个答案:

没有答案
相关问题