Это мой текущий код для memorygame, над которым я работаю. Я использую чистый JavaScript, и у меня возникают проблемы с функцией onclick моих тегов. Я хочу, чтобы игра заменила щелкнутое изображение (знак вопроса) изображением, присвоенным номеру числа щелкнувших изображений, создав тем самым эффект флип.Javascript памяти игра - обмен изображениями
В настоящее время я получаю то, что независимо от того, где я нажимаю, только последнее изображение заменяется.
"use strict"
var newDiv = null;
var innerDiv = null;
var table = null;
var row = null;
var cell = null;
var aTag = null;
var image = null;
// Use this to put class names on the images.
var boxCounter = 0;
// Static memory-object.
var Memory = {
memoryArray: [],
init: function (e) {
// Calls a separate js-file which generates a random numbers.
Memory.memoryArray = RandomGenerator.getPictureArray(4, 4);
// Calls the rendering method
Memory.renderArray(Memory.memoryArray);
},
renderArray: function (myArray) {
// Creates and places div-tags in the HTML-document.
newDiv = document.createElement("div");
document.getElementsByTagName("body")[0].appendChild(newDiv);
innerDiv = document.createElement("div");
newDiv.appendChild(innerDiv);
// Creates a table and places it in the HTML-document.
table = document.createElement("table");
table.border = 1;
// Generates rows and cells, swap the 4's to change the size of the gameboard.
for (var i = 0; i < 4; ++i) {
row = document.createElement("tr");
table.appendChild(row);
// Creates a cell, each with its own respective random number.
for (var j = 0; j < 4; ++j) {
cell = document.createElement("td");
// Adds a "Question-mark"-picture to the cell and places them in a-tags.
image = document.createElement("img");
image.className = myArray[i * 4 + j];
image.src = "https://github.com/1dv403/1dv403-laborationer/blob/master/3-gameon/memory/pics/0.png?raw=true";
aTag = document.createElement("a");
aTag.onclick = function() {
Memory.flipTile(image.className);
};
// Places the pictures in the document, along with its random number for easier testing purposes.
aTag.appendChild(document.createTextNode(myArray[i * 4 + j]));
aTag.appendChild(image);
cell.appendChild(aTag);
row.appendChild(cell);
};
};
innerDiv.appendChild(table);
},
flipTile: function (imageClass) {
console.log(imageClass);
// This should flip the tiles if the number matches the class name.
if (imageClass == 1) {
image.src = "https://github.com/1dv403/1dv403-laborationer/blob/master/3-gameon/memory/pics/1.png?raw=true";
};
if (imageClass == 2) {
image.src = "https://github.com/1dv403/1dv403-laborationer/blob/master/3-gameon/memory/pics/2.png?raw=true";
};
if (imageClass == 3) {
image.src = "https://github.com/1dv403/1dv403-laborationer/blob/master/3-gameon/memory/pics/3.png?raw=true";
};
if (imageClass == 4) {
image.src = "https://github.com/1dv403/1dv403-laborationer/blob/master/3-gameon/memory/pics/4.png?raw=true";
};
if (imageClass == 5) {
image.src = "https://github.com/1dv403/1dv403-laborationer/blob/master/3-gameon/memory/pics/5.png?raw=true";
};
if (imageClass == 6) {
image.src = "https://github.com/1dv403/1dv403-laborationer/blob/master/3-gameon/memory/pics/6.png?raw=true";
};
if (imageClass == 7) {
image.src = "https://github.com/1dv403/1dv403-laborationer/blob/master/3-gameon/memory/pics/7.png?raw=true";
};
if (imageClass == 8) {
image.src = "https://github.com/1dv403/1dv403-laborationer/blob/master/3-gameon/memory/pics/8.png?raw=true";
};
}
};
window.onload = Memory.init;
Тот же результат, что и код Paul S., работает, чтобы переключаться между изображениями, но это только последнее изображение таблицы, которое заменяется, а не тот, на который вы нажимаете! – Lemonmuncher
С вашим редактированием он работал так, как предполагалось. Я только передал переменную изображения и изнутри flipTile я затем проверил image.className, а затем изменил image.src, если он совпадает с числами. – Lemonmuncher