在记忆游戏Javascript中的瓷砖悬停状态

时间:2016-08-01 17:25:40

标签: javascript

我为Khan Academy提供了一个记忆游戏的JavaScript代码,我不知道如何在鼠标悬停时更改颜色。作为一个测试,当鼠标在鼠标点击功能的“if(tiles [i] .isUnderMouse(mouseX,mouseY))”中,我试图在一个图块上绘制一个星形,但当然只有当鼠标是单击并且因为图块处于绘图功能中,所以在下一次单击后,星形将被放置在新的图块集之后。我甚至不知道从哪里开始这样做。谁能帮我?

//Card face down image variable
var fdImage = image(getImage("avatars/questionmark"), this.x, this.y, this.width, this.width);


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

Tile.prototype.drawFaceDown = function() {
    fill(214, 247, 202);
    strokeWeight(2);
    rect(this.x, this.y, this.width, this.width, 10);
    image(getImage("avatars/questionmark"), this.x, this.y, this.width, this.width);
    this.isFaceUp = false;
};

Tile.prototype.drawFaceUp = function() {
    fill(214, 247, 202);
    strokeWeight(2);
    rect(this.x, this.y, this.width, this.width, 10);
    image(this.face, this.x, this.y, this.width, this.width);
    this.isFaceUp = true;
};

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


        }
    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 = [
    //saplings
    getImage("avatars/leafers-seed"),
    getImage("avatars/leafers-seedling"),
    getImage("avatars/leafers-sapling"),
    getImage("avatars/leafers-tree"),
    getImage("avatars/leafers-ultimate"),
    getImage("avatars/piceratops-seed"),
    getImage("avatars/piceratops-seedling"),
    getImage("avatars/piceratops-sapling"),
    getImage("avatars/piceratops-tree"),
    getImage("avatars/piceratops-ultimate"),
    getImage("avatars/aqualine-seed"),
    getImage("avatars/aqualine-seedling"),
    getImage("avatars/aqualine-sapling"),
    getImage("avatars/aqualine-tree"),
    getImage("avatars/aqualine-ultimate"),
    //figures
    getImage("avatars/marcimus"),
    getImage("avatars/mr-pants"),
    getImage("avatars/mr-pink"),
    getImage("avatars/old-spice-man"),
    getImage("avatars/orange-juice-squid"),
    getImage("avatars/purple-pi"),
    getImage("avatars/spunky-sam"),
    //robots
    getImage("avatars/robot_female_1"),
    getImage("avatars/robot_female_2"),
    getImage("avatars/robot_female_3"),
    getImage("avatars/robot_male_1"),
    getImage("avatars/robot_male_2"),
    getImage("avatars/robot_male_3"),
    //important figures
    getImage("creatures/Hopper-Happy"),
    getImage("creatures/Hopper-Cool"),
    getImage("creatures/Hopper-Jumping"),
    getImage("creatures/OhNoes"),
    getImage("creatures/BabyWinston"),
    getImage("creatures/Winston"),
    //rpg material
    getImage("space/beetleship"),
    getImage("space/healthheart"),
    getImage("space/octopus"),
    getImage("space/planet"),
    getImage("space/rocketship"),
    getImage("space/star"),
];

// 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 we need to randomize the array
selected.sort(function() {
    return 0.5 - Math.random();
});

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

background(255, 255, 255);

// Now draw them face up
for (var i = 0; i < tiles.length; i++) {
    tiles[i].drawFaceDown();
}

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

mouseClicked = function() {
    for (var i = 0; i < tiles.length; i++) {
        //
        if (tiles[i].isUnderMouse(mouseX, mouseY)) {

        // 
            if (flippedTiles.length < 2 && !tiles[i].isFaceUp) {
                tiles[i].drawFaceUp();
                flippedTiles.push(tiles[i]);
                if (flippedTiles.length === 2) {
                    numTries++;
                    if (flippedTiles[0].face === flippedTiles[1].face) {
                        flippedTiles[0].isMatch = true;
                        flippedTiles[1].isMatch = true;
                    }
                    delayStartFC = frameCount;
                    loop();
                }
            } 
        }
    }
    var foundAllMatches = true;
    for (var i = 0; i < tiles.length; i++) {
        foundAllMatches = foundAllMatches && tiles[i].isMatch;
    }
    if (foundAllMatches) {
        fill(0, 0, 0);
        textSize(20);
        text("You found them all in " + numTries + " tries!", 20, 375);
    }
};

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

1 个答案:

答案 0 :(得分:0)

为了在此Khan Academy挑战中向悬停状态添加悬停状态,您必须先向Tile对象添加新方法

Tile.prototype.hoverState = function() {
    fill(150, 247, 202);
    strokeWeight(2);
    rect(this.x, this.y, this.width, this.width, 10);
    image(getImage("avatars/leaf-green"), this.x, this.y, this.width, this.width);
    this.isFaceUp = false;
};

之后你需要像这样使用mouseMoved:

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();
        }
    }
};