I'm having the following code that should add the EventListener for the click to every cell of the table, but if I put the name of function with the parameter, the function seems to start as the interpreter meets the inscruction to add the eventListener. How could I solve this?
function startGame() {
for(var i = 1; i <= 16; i++) {
var cellValue = Math.floor(Math.random() * 16);
while(isPresent(cellValue)) {
cellValue = Math.floor(Math.random() * 16);
}
numbers[cellValue] = true;
var cell = document.getElementById("cell" + i);
cell.addEventListener("click", selectCell(cell), false);
if(cellValue == 0) {
cell.innerHTML = "";
cell.setAttribute("class", "emptyCell");
} else {
cell.removeAttribute("class");
cell.innerHTML = cellValue;
}
}
}
function selectCell(cell) {
if(!isCellSelected) {
cellSelected = cell;
isCellSelected = true;
} else if(cell == cellSelected) {
cellSelected = null;
isCellSelected = false;
} else {
var cellID = cell.id;
var effectiveID = parseInt(cellID.substring(4, 6));
if(isGranted(effectiveID)) {
//COMPLETARE METODO
} else {
window.alert("Selezionare solo una cella adiacenta alla cella già selezionata.");
}
}
}
function isGranted(id) {
var result;
var existentEffectiveID = parseInt(cellSelected.id.substring(4, 6));
//Se la cella selezionata è la successiva o precedente in termini orizzontali o verticali allora si puo' selezionare
if(id == existentEffectiveID - 4 || id == existentEffectiveID + 4 || id == existentEffectiveID + 1 || id == existentEffectiveID - 1) {
result = true;
} else {
result = false;
}
return result;
}
Aucun commentaire:
Enregistrer un commentaire