我正在編寫一個叫做Chomp的遊戲,我已經完成了遊戲本身,但是我有一些可以修復的問題。每當你點擊這個遊戲中的一個正方形(除了綠色正方形)時,包括被點擊的正方形在內的某些正方形應該會消失並永遠消失。擦除想要重現的單元
但是,當前,當用戶點擊一個正方形時,它會創建整個表格的殘留圖像。例如,this是一個點擊中心廣場作爲第一步後的樣子,如您在第二步here中看到的那樣,第一步中的棋子會短暫地重現,只是很快會再次淡出。
每當方塊被擦除時,它們都不應該像現在一樣重新出現,我不確定是什麼導致了這種行爲。但我相信這可能是由於html元素沒有被正確刪除。我怎樣才能讓已經褪色的方格不再出現?
這裏是我的HTML gameTable
<!--game table-->
<table id="gameTable">
</table>
,我的處理單元點擊JavaScript函數,和褪色元素
fadeOut = function fadeOut(state) {
// Make fadeOut unavailable until the whole fade-out is finished.
fadeOut.isAvailableToRun = false;
// Update the distance moved and apply it to the element.
state.distance += state.distanceIncrement;
state.element.style.top = state.distance + 'px'; //move up by pixels
// Update the opacity and apply it to the element.
state.opacity += state.opacityIncrement;
state.element.style.opacity = state.opacity;
//if opacity is > 0 , fade it out into the ethers
if (state.opacity > 0) {
// If the element is still showing, wait a bit and then continue fading it.
setTimeout(function() {
fadeOut(state);
}, state.timeIncrement);
}
};
//contains values to use for fadeOut
cellClick = function (cell) {
var a, x, y;
//make all cells above, to the right, and inbetween fade a clicked cell fade
for (a = 0; a < tableData.length; a += 1) {
//get x,y coordinates from tableData
x = cell.pos.x;
y = cell.pos.y;
//erase position in index compared to the clicked position
if (tableData[a].pos.x <= x && tableData[a].pos.y >= y) {
//remove clickability of cells other than clicked cell
tableData[a].onclick = null;
//apply fadeOut effect to cells other than clicked cell
fadeOut({
distance: 0, // initial distance from start
distanceIncrement: 1, // number of pixels to move each timer tick
element: tableData[a], // element to move and fade (cell, element passed as a parameter to the click cell function)
opacity: 1, // initial opacity
opacityIncrement: -0.01, // how much to fade each timer tick
pause: 1000, // milliseconds to pause after completed fade
timeIncrement: 10 // milliseconds for each timer tick
});
}
}
Here是我的代碼在它的全部,與現場演示所以你可以很容易地看到自己的問題。
謝謝,我從Padge的建議做了一個類似的解決方案,但你的是更清潔。感謝您的幫助! – user3335607