[英]javascript - How to randomly activate multiple cells in grid
I work on one game and I stack in one problem. 我从事一项游戏,却遇到了一个问题。 Look at code and test it.
查看代码并对其进行测试。
var createGrid=function(x,y){ var arrY = new Array(), arrX, container = $(".table"); i=1; for(var iy=0; iy<y; iy++){ arrX = new Array(); for(var ix=0; ix<x; ix++){ arrX[ix]='<div class="cell">'+i+'</div>'; i++; } arrY[iy]='<div class="row">'+arrX.join("\\r\\n")+'</div>'; } container.append(arrY.join("\\r\\n")); }; // call function (function($){ // create grid createGrid(Math.ceil($(window).width()/50),Math.ceil($(window).height()/50)); // setup on ready $(document).ready(function(){ var cell= $(".cell"), maxCell = cell.length, // find random cell and setup randomActivate = function(){ $(".cell.active").removeClass("active"); var active=Math.round(Math.random()*(maxCell-1)); cell.eq(active).addClass("active"); }; // start random cell randomActivate(); // loop random cell setInterval(function(){ randomActivate(); }, 300); }); }(jQuery));
body{ padding:0; margin:-1px; clear:both; overflow:hidden; position:relative; } .cell {display:table-cell; border-right: 1px solid #000000; border-bottom: 1px solid #000000; width: 50px; height: 50px; text-align:center; vertical-align:middle; font-size:22px; font-weight:900; color:#FFF; } .row { display:table-row; clear: both; overflow: hidden; } .row:hover{ background:#e9e9e9; } .row:hover>.cell{ color:#e9e9e9; } .row>.cell:hover, .active{ background:#f00; } .table { position:absolute; top:0; left:0; bottom:0; right:0; z-index:0; border-left: 1px solid #000000; border-top: 1px solid #000000; display:table; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="table"></div>
The problem where I stack is how to display in the same time multiple cells in grid? 我堆叠的问题是如何在网格中同时显示多个单元格?
For now, like you see, only one cell display number per interval. 现在,就像您看到的那样,每个时间间隔仅显示一个单元格编号。 I need to add more intervals with different time what will made effect like randomly nasted numbers like show and hide.
我需要在不同的时间添加更多间隔,这将产生效果,例如显示和隐藏的随机插入数字。 Is hard to explain becouse I can't find example.
很难解释,因为我找不到例子。
In the same time can be more than one active cell but every interval to have control of own active cell. 在同一时间可以有多个活动单元,但每个间隔都可以控制自己的活动单元。
You will have questions for me, please, shoot. 请问我一些问题,请射击。
One simple solution would be to create another randomActivate
function, like randomActivate01
and have it light up other cells. 一种简单的解决方案是创建另一个
randomActivate
函数,例如randomActivate01
并使它照亮其他单元格。
You can also apply a different CSS
class to the cells lit up by the second randomActivate
so you could have them appear yellow, or orange, instead of the same red. 您还可以对第二个
randomActivate
点亮的单元格应用不同的CSS
类,以便使它们显示为黄色或橙色,而不是相同的红色。
Would that be the answer you are looking for? 这就是您要寻找的答案吗?
Your randomActivate
function starts with $(".cell.active").removeClass("active");
您的
randomActivate
函数以$(".cell.active").removeClass("active");
which inactivates the currently active cell. 这会停用当前活动的单元格。
If you want to activate the entire grid in random order, just remove that call. 如果要以随机顺序激活整个网格,只需删除该调用即可。
If you want something else, this is still where I'd recommend you to alter the code to suit your purposes. 如果您还有其他需要,我仍然建议您更改代码以适合您的目的。
Instead of removing the active
class from all active cells like you are now, you could set a timeout to remove it from that specific class after some time, for instance. 除删除的
active
从所有活性细胞类喜欢你现在的样子,你可以设置一个超时一段时间后,从该特定类别中删除它,例如。
Random reveal example 随机显示示例
Here I also decrement maxCell
on each iteration and use that to access an index not on all cells, but only on those that are not already active. 在这里,我还在每次迭代中递减
maxCell
,并使用它来访问索引,而不是在所有单元上,而不是仅在那些尚未激活的单元上。 Otherwise it could potentially take very long time until the last cell were to be randomized. 否则,将最后一个单元随机化可能会花费很长时间。 I also stop the timeout when there are no inactive cells left.
当没有闲置的单元格时,我也会停止超时。
Random timeouts example 随机超时示例
Here I am setting a random timeout for each activated cell, rather than removing all activated cells at all iterations. 在这里,我为每个激活的单元设置一个随机超时,而不是在所有迭代中都删除所有激活的单元。
If none of these are anywhere near your desired behavior, please specify in greater detail what you're after. 如果这些都不是您想要的行为的任何地方,请详细说明您要采取的行动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.