为什么我在这个内存瓦片javascript项目中的悬停状态不起作用?

为什么我在这个内存瓦片javascript项目中的悬停状态不起作用?,javascript,project,khan-academy,Javascript,Project,Khan Academy,我正在尝试将悬停状态添加到Khan Academy上的内存块项目中。我相信我已经在底部插入了正确的代码,以更改瓷砖的颜色,但是当我测试代码时,它只会在快速悬停时闪烁我想要显示的图像,然后它就会消失。每次尝试显示悬停状态时,似乎都有一些绘图函数在它上面绘图。请帮帮我,我已经在这上面呆了很久了 var Tile = function(x, y, face) { this.x = x; this.y = y; this.width = 70; this.face = f

我正在尝试将悬停状态添加到Khan Academy上的内存块项目中。我相信我已经在底部插入了正确的代码,以更改瓷砖的颜色,但是当我测试代码时,它只会在快速悬停时闪烁我想要显示的图像,然后它就会消失。每次尝试显示悬停状态时,似乎都有一些绘图函数在它上面绘图。请帮帮我,我已经在这上面呆了很久了

var Tile = function(x, y, face) {
    this.x = x;
    this.y = y;
    this.width = 70;
    this.face = face;
    this.isFaceUp = false;
    this.isMatch = false;
};

Tile.prototype.draw = function() {
    fill(214, 247, 202);
    strokeWeight(2);
    rect(this.x, this.y, this.width, this.width, 10);
    if (this.isFaceUp) {
        image(this.face, this.x, this.y, this.width, this.width);
    } else {
        image(getImage("avatars/leaf-green"), this.x, this.y, this.width, this.width);
    }
};

Tile.prototype.isUnderMouse = function(x, y) {
    return x >= this.x && x <= this.x + this.width  &&
        y >= this.y && y <= this.y + this.width;
};

// Global config
var NUM_COLS = 5;
var NUM_ROWS = 4;

// Declare an array of all possible faces
var faces = [
    getImage("avatars/leafers-seed"),
    getImage("avatars/leafers-seedling"),
    getImage("avatars/leafers-sapling"),
    getImage("avatars/leafers-tree"),
    getImage("avatars/leafers-ultimate"),
    getImage("avatars/marcimus"),
    getImage("avatars/mr-pants"),
    getImage("avatars/mr-pink"),
    getImage("avatars/old-spice-man"),
    getImage("avatars/robot_female_1")
];

// Make an array which has 2 of each, then randomize it
var possibleFaces = faces.slice(0);
var selected = [];
for (var i = 0; i < (NUM_COLS * NUM_ROWS) / 2; i++) {
    // Randomly pick one from the array of remaining faces
    var randomInd = floor(random(possibleFaces.length));
    var face = possibleFaces[randomInd];
    // Push twice onto array
    selected.push(face);
    selected.push(face);
    // Remove from array
    possibleFaces.splice(randomInd, 1);
}

// Now shuffle the elements of that array
var shuffleArray = function(array) {
    var counter = array.length;

    // While there are elements in the array
    while (counter > 0) {
        // Pick a random index
        var ind = Math.floor(Math.random() * counter);
        // Decrease counter by 1
        counter--;
        // And swap the last element with it
        var temp = array[counter];
        array[counter] = array[ind];
        array[ind] = temp;
    }
};
shuffleArray(selected);

// Create the tiles
var tiles = [];
for (var i = 0; i < NUM_COLS; i++) {
    for (var j = 0; j < NUM_ROWS; j++) {
        var tileX = i * 78 + 10;
        var tileY = j * 78 + 40;
        var tileFace = selected.pop();
        tiles.push(new Tile(tileX, tileY, tileFace));
    }
}

background(255, 255, 255);

var numTries = 0;
var numMatches = 0;
var flippedTiles = [];
var delayStartFC = null;

mouseClicked = function() {
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        if (tile.isUnderMouse(mouseX, mouseY)) {
            if (flippedTiles.length < 2 && !tile.isFaceUp) {
                tile.isFaceUp = true;
                flippedTiles.push(tile);
                if (flippedTiles.length === 2) {
                    numTries++;
                    if (flippedTiles[0].face === flippedTiles[1].face) {
                        flippedTiles[0].isMatch = true;
                        flippedTiles[1].isMatch = true;
                        flippedTiles.length = 0;
                        numMatches++;
                    }
                    delayStartFC = frameCount;
                }
            } 
            loop();
        }
    }
};

draw = function() {
    background(255, 255, 255);
    if (delayStartFC && (frameCount - delayStartFC) > 30) {
        for (var i = 0; i < tiles.length; i++) {
            var tile = tiles[i];
            if (!tile.isMatch) {
                tile.isFaceUp = false;
            }
        }
        flippedTiles = [];
        delayStartFC = null;
        noLoop();
    }

    for (var i = 0; i < tiles.length; i++) {
        tiles[i].draw();
    }

    if (numMatches === tiles.length/2) {
        fill(0, 0, 0);
        textSize(20);
        text("You found them all in " + numTries + " tries!", 20, 375);

    }
};

noLoop();

Tile.prototype.hoverState = function() {
    fill(74, 122, 128);
    strokeWeight(2);
    rect(this.x, this.y, this.width, this.width, 10);
    image(getImage("avatars/leaf-yellow"), this.x, this.y, this.width, this.width);
    this.isFaceUp = false;
};

mouseMoved = function() {
    for (var i = 0; i <= tiles.length; i++) {
        if (tiles[i].isUnderMouse(mouseX, mouseY) && tiles[i].isFaceUp) {
            tiles[i].hoverState();
        } else if (tiles[i].isFaceUp) {
            tiles[i].drawFaceUp();
        } else {
            tiles[i].drawFaceDown();
        }
    }
};
var Tile=函数(x,y,面){
这个.x=x;
这个。y=y;
这个宽度=70;
这张脸=脸;
this.isFaceUp=false;
this.isMatch=false;
};
Tile.prototype.draw=函数(){
填充(214247202);
冲程重量(2);
rect(this.x,this.y,this.width,this.width,10);
if(this.isFaceUp){
图像(this.face,this.x,this.y,this.width,this.width);
}否则{
图像(getImage(“化身/绿叶”)、this.x、this.y、this.width、this.width);
}
};
Tile.prototype.isUnderMouse=函数(x,y){
返回x>=this.x&&x=this.y&&y 0){
//选择一个随机索引
var ind=Math.floor(Math.random()*计数器);
//将计数器减少1
计数器--;
//并用它交换最后一个元素
var temp=数组[计数器];
数组[计数器]=数组[ind];
数组[ind]=temp;
}
};
shuffleArray(选定);
//创建瓷砖
var-tiles=[];
对于(变量i=0;i30){
对于(变量i=0;i对于(var i=0;我不确定您是否查看了
mouseover
事件?能否显示完整的代码?@JavascriptDev这是完整的代码-在khan academy中,所有内容都是纯javascript的,此项目没有额外的HTML或CSS或链接-所有内容都是由javascript@Pogrindis我已经调查过了,对何有点困惑不过我还是要用它。这门课上的每件事都学得很快,让我很困惑。我可以很容易地阅读代码(这对我来说很有意义),但我很难从内存/逻辑中拼凑出来。但是,第一行?你在复制时错过了吗?