(color_array[i])是函数外的值(会更新)。如下的监听触发,不会改变(color_array[i]),varcanvas=document.getElementById("mycanvas");varctx=canvas.getContext("2d");//console.log(ctx);canvas.width=canvas.height=200;ctx.beginPath();varscore=0,time=60,flag=0;//定义初始值,时间varcolor_array=["red","white","black","blue","green","DarkSlateGray3","DarkSlateGray","DarkOliveGreen4"];ctx.moveTo(0,0);ctx.lineTo(100,0);ctx.lineTo(50,50);vargameState=setInterval(changeColor,1000);functionchangeColor(){if(!time){clearInterval(gameState);}vardomTime=document.querySelector(".time"),domScore=document.querySelector(".score");domTime.innerHTML=time;//插入时间vari=time%8;//根据time值顺序遍历数组//触摸监听canvas.addEventListener("touchstart",function(){if(color_array[i]==='white'){console.log(i);flag=1;//设置状态,注意这里有多次触发}},false);if(flag==1){score++;domScore.innerHTML=score;flag=0;//清楚状态,重新累加积分}ctx.fillStyle=color_array[i];//changecolorctx.fill();i++;time--;}函数为什么console.log(i)一直没不变。。我想过传外面的参,不过怎么想都不知道
添加回答
举报
0/150
提交
取消