// 运动操作 moveUpAndDown: function() { var that = this; // 定时器随机定义good|bad老鼠个数,以及需要显示的个数 that.moveTime = setInterval(function() { for (var i = 0, j = that.mouses.length; i < j; ++i) { that.mouses[i].setAttribute('clicked', '0'); that.mouses[i].className = 'good active'; that.mouses[i].style.display = 'none'; } badNum = that.getRandom(0, 8); for (var i = 0; i < badNum; i++) { that.mouses[that.getRandom(0, 8)].className = 'bad active'; } showNum = that.getRandom(0, 8); for (var i = 0; i < showNum; i++) { that.mouses[that.getRandom(0, 8)].style.display = 'block'; } }, 2000); },
使用定时器,定时器的循环与CSS中的动画设置一致,保证循环连贯性
设置class为good 即可定义出一只好老鼠,同理bad 为坏老鼠
在开始游戏,进行调用时,设置class为active 即可让老鼠运动起来
对于打老鼠的操作,要注意到只有运动的老鼠才能点击,每只老鼠只能点击一次