我正在做一个四连环游戏,现在我正试图记录单击该列的某个列中的每个div的数组。我能够记录从第一个索引到最后一个数组。现在,我不想将数组从最后一个索引记录到第一个索引。我的功能看起来像这样。
`function dropChip(col) {
for(let i = col.length-1; i < col.length; i--) {
console.log(col[i]);
}
}`
让我困惑的是我可以去console.log(col);并查看所有六个项目。但是我不能从相反的角度看待它们?(因为我需要检查底部div是否为空,因为需要堆叠筹码。而且将DOM用于游戏逻辑是一个好主意吗?我这样做是在我制作的先前版本被告知设计不好,但我不知道另一种方法或如何用JS进行操作)以这种方式查看它们会破坏游戏?关于我在做什么错的任何想法吗?谢谢:)
const board = document.querySelectorAll(".slot");
for(let i = 0; i < board.length; i++) {
board[i].addEventListener('click', selectColumn);
}
function selectColumn(evt) {
let column = evt.target.id;
let columnArray = [];
for(let i=0; i < board.length; i++) {
if(column === board[i].id) columnArray.push(board[i]);
}
dropChip(columnArray);
}
function dropChip(col) {
for(let i = col.length-1; i < col.length; i--) {
console.log(col[i]);
}
}
/** {
border: 1px solid red;
}*/
*, *:before, *:after {
box-sizing: inherit;
}
html {
box-sizing: border-box;
}
html, body {
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: flex-end;
min-height: 100vh;
background-color: #e6e9ef;
flex-direction: column;
}
.board-wrapper {
/*display: flex;*/
margin: 0 auto;
padding-top: 100px;
position: relative;
overflow: hidden;
}
.board {
display: flex;
background-color: #00c;
flex-wrap: wrap;
max-width: 706px;
padding: 3px;
/*margin: auto;*/
}
.board > div {
width: 100px;
height: 100px;
background-color: blue;
border: 3px solid #00c;
cursor: pointer;
position: relative;
}
.board > div:before {
content: "";
position: absolute;
top: 7px;
left: 7px;
background-color: #00c;
width: 80px;
height: 80px;
border-radius: 50%;
box-shadow: inset 0px 0px 13px #0606aa;
}
.board > div:after {
content: "";
position: absolute;
z-index: 99;
left: 0;
right: 0;
top: -999em;
bottom: -999em;
transition: all .5s ease;
background-color: transparent;
}
.board > div:hover:after {
background-color: #00c;
}
.board > div.counter {
/*display: block;
padding: 0;*/
position: absolute;
top: 0;
left: 0;
right: 0;
width: auto;
height: 100px;
border: none;
background-color: #e6e9ef;
z-index: 100;
}
.board > div.counter:after {
/*content: "";*/
/*display: block;*/
/*padding: 0;*/
height: 80px;
width: 80px;
background-color: red;
top: 0;
/*left: 0;
right: auto;*/
border-radius: 50%;
transition: transform .5s ease, opacity .2s ease;
/*transform: translateX(0);*/
z-index: 100;
opacity: 0;
}
.board > div.counter:before {
content: "";
display: block;
padding: 0;
position: absolute;
top: 15px;
left: 15px;
right: auto;
height: 50px;
width: 50px;
border-radius: 50%;
background-color: red;
box-shadow: inset 0px 0px 20px #cc0000;
z-index: 101;
transition: transform .5s ease, opacity .2s ease;
/*transform: translateX(0);*/
opacity: 0;
}
.board > div:nth-of-type(7n+1):hover ~ .counter:after{transform:translateX(14px);opacity: 1;}
.board > div:nth-of-type(7n+2):hover ~ .counter:after{transform:translateX(114px);opacity: 1;}
.board > div:nth-of-type(7n+3):hover ~ .counter:after{transform:translateX(214px);opacity: 1;}
.board > div:nth-of-type(7n+4):hover ~ .counter:after{transform:translateX(314px);opacity: 1;}
.board > div:nth-of-type(7n+5):hover ~ .counter:after{transform:translateX(414px);opacity: 1;}
.board > div:nth-of-type(7n+6):hover ~ .counter:after{transform:translateX(514px);opacity: 1;}
.board > div:nth-of-type(7n+7):hover ~ .counter:after{transform:translateX(614px);opacity: 1;}
.board > div:nth-of-type(7n+1):hover ~ .counter:before{transform:translateX(14px);opacity: 1;}
.board > div:nth-of-type(7n+2):hover ~ .counter:before{transform:translateX(114px);opacity: 1;}
.board > div:nth-of-type(7n+3):hover ~ .counter:before{transform:translateX(214px);opacity: 1;}
.board > div:nth-of-type(7n+4):hover ~ .counter:before{transform:translateX(314px);opacity: 1;}
.board > div:nth-of-type(7n+5):hover ~ .counter:before{transform:translateX(414px);opacity: 1;}
.board > div:nth-of-type(7n+6):hover ~ .counter:before{transform:translateX(514px);opacity: 1;}
.board > div:nth-of-type(7n+7):hover ~ .counter:before{transform:translateX(614px);opacity: 1;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Connect Four</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="board-wrapper">
<div class="board">
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="slot" id="col-1"></div>
<div class="slot" id="col-2"></div>
<div class="slot" id="col-3"></div>
<div class="slot" id="col-4"></div>
<div class="slot" id="col-5"></div>
<div class="slot" id="col-6"></div>
<div class="slot" id="col-7"></div>
<div class="counter"></div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
答案 0 :(得分:1)
您要更改for
循环:
for (let i = 0; i < col.length; i++) {...}
答案 1 :(得分:0)
看这行:
for(let i = col.length-1; i < col.length; i--) {
我从最大值开始,一直下降到无穷...
停止条件不应为i < col.length
(永远为真)-您希望它在到达最后一列(索引为零)时停止!