我有一个JavaScript错误,我不明白

时间:2017-11-02 18:23:28

标签: javascript

我在Javascript中有一个问题,我正在尝试制作卡片匹配游戏,我想使用图片而不是使用字母和数字我看到一个单独的帖子告诉我使用一个名为“getimage”的东西,它被使用像这样

var dogs = [
getimage("images/1.png"),
getimage("images/1.png"),
getimage("images/2.png"),
getimage("images/2.png"),
getimage("images/3.png"),
getimage("images/3.png")
];

所以,输出告诉我在定义之前我使用了getImage,我不明白为什么因为我使用的引用没有定义它似乎工作(他们的用途不同)

任何人都可以帮我吗?

完整代码:

var Tile = function(x, y, dogs) {
this.x = x;
this.y = y;
this.dogs = dogs;
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("images/6.png"), this.x, this.y, this.width, this.width);
};

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


var dogs = [
getimage("images/1.png"),
getimage("images/1.png"),
getimage("images/2.png"),
getimage("images/2.png"),
getimage("images/3.png"),
getimage("images/3.png")

];
var memory_values = [];
var memory_tile_ids = [];
var tiles_flipped = 0;
Array.prototype.memory_tile_shuffle = function(){
var i = this.length, j, temp;
while(--i > 0){
    j = Math.floor(Math.random() * (i+1));
    temp = this[j];
    this[j] = this[i];
    this[i] = temp;
 }
}

function newBoard(){
tiles_flipped = 0;
var output = '';
memory_array.memory_tile_shuffle();
for(var i = 0; i < memory_array.length; i++){
    output += '<div id="tile_'+i+'" 
onclick="memoryFlipTile(this,\''+memory_array[i]+'\')"></div>';
}
document.getElementById('memory_board').innerHTML = output;
}




function memoryFlipTile(tile,val){
if(tile.innerHTML == "" && memory_values.length < 2){
    tile.style.background = '#FFF';
    tile.innerHTML = val;
    if(memory_values.length == 0){
        memory_values.push(val);
        memory_tile_ids.push(tile.id);
    } else if(memory_values.length == 1){
        memory_values.push(val);
        memory_tile_ids.push(tile.id);
        if(memory_values[0] == memory_values[1]){
            tiles_flipped += 2;
            // Clear both arrays
            memory_values = [];
            memory_tile_ids = [];
            // Check to see if the whole board is cleared
            if(tiles_flipped == memory_array.length){
                alert("Yay! You did it!");
                document.getElementById('memory_board').innerHTML = "";
                newBoard();
            }
        } else {
            function flip2Back(){
                // Flip the 2 tiles back over
                var tile_1 = document.getElementById(memory_tile_ids[0]);
                var tile_2 = document.getElementById(memory_tile_ids[1]);
                tile_1.style.background = 'url(tile_bg.jpg) no-repeat';
                tile_1.innerHTML = "";
                tile_2.style.background = 'url(tile_bg.jpg) no-repeat';
                tile_2.innerHTML = "";
                // Clear both arrays
                memory_values = [];
                memory_tile_ids = [];
            }
            setTimeout(flip2Back, 700);
      }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

检查您的变量名称。您正在使用小写和camelcase的组合(getImage vs getimage)。

答案 1 :(得分:0)

创建一个名为getimage的函数,它可以满足您的需要,或者将插件或库合并到创建该函数的位置,以便您可以使用它。如果您使用的是插件/库,请查看文档以确保在致电getimage时使用正确的语法。