我写的一个打字游戏为什么setInterval不能执行move方法呢?
代码如下:
HTMLCode:
<html>
<head>
<title>打字</title>
<link type="text/css" href="./css/objMain.css" rel="stylesheet">
<script src="./js/objClickWords.js"></script>
</head>
<body onkeypress="makeWordFromKeyBoard()">
<div id="outer">
<div id="inner_top">
<!--主显示区域即字母下降区域-->
</div>
<div id="inner_bottom">
<!--状态和控制面板区域-->
分数:<span id="cores">0</span>
難度:<select onchange="changeGameLevel()" id="hard">
<option value="700">7</option>
<option value="600">6</option>
<option value="500">5</option>
<option value="400">4</option>
<option value="300">3</option>
<option value="200">2</option>
<option value="100">1</option>
</select>
运行状态:<span id="run_status">停止中.......</span>
<button onclick="startGameFromKeyBoard()">begin</button>
<button onclick="pauseGame()">pause</button>
</div>
</div>
</body>
</html>
jsCode:
//盒子或容器
function Box(boxId,boxName){
var B_this=this;
B_this.id=boxId;
B_this.bName=boxName;
B_this.boxSize;//容器大小
B_this.isExist=false;//盒子是否在引擎上
B_this.htmlNodeObj;
{
//alert("999");
if(B_this.id==null||B_this.id==undefined){
alert("盒子参数未初始化1!");
return;
}
if(B_this.bName==null||B_this.bName==undefined){
alert("盒子参数未初始化2!");
return;
}
B_this.htmlNodeObj=document.createElement("label");
B_this.htmlNodeObj.id=B_this.id;
B_this.htmlNodeObj.innerHTML=String.fromCharCode(B_this.bName);
B_this.htmlNodeObj.style.position="absolute";
B_this.htmlNodeObj.style.top=0+"px";
}
B_this.getHtmlNodeObj=function(){//获得label节点对象
return B_this.htmlNodeObj;
}
B_this.setBName=function(bxName){//为盒子命名
B_this.bName=bxName;
}
B_this.getBName=function(){//获得盒子的名字
return B_this.bName;
}
B_this.setId=function(bId){//给盒子一个id
B_this.id=bId;
}
B_this.getId=function(){//获得盒子的id
return B_this.id;
}
B_this.getBoxSize=function(){//获得盒子的大小
return B_this.boxSize;
}
B_this.setBoxSize=function(size){//设置盒子的大小
B_this.boxSize=size;
}
B_this.setIsExist=function(isOrExist){//判断盒子是否存在
if(isOrExist){
document.getElementById("inner_top").appendChild(B_this.htmlNodeObj);
B_this.htmlNodeObj.style.display="block";
// alert("我进div了");
}else{
document.getElementById("inner_top").removeChild(B_this.htmlNodeObj);
}
}
B_this.getIsExist=function(){//获得盒子的状态
return B_this.isExist;
}
}
//传送引擎
function EngineTransport(){
var E_this=this;
E_this.cores=0;//计分器
E_this.engineContain=new Array();//引擎容器
E_this.engineLength=parseInt(document.getElementById("inner_top").offsetHeight);//传送引擎的传送长度
E_this.engineWidth=parseInt(document.getElementById("inner_top").offsetWidth);//传送引擎的宽度
E_this.engineLevel=3000;//传送引擎的档位,20>>40>>>60>>>>80
E_this.engineRunStatus=0;//传送引擎的运行状态:0--停止,1--开始,2--暂停.
E_this.engineClock;//传送引擎的传送速度
E_this.getEngineContain=function(){//获得引擎容器
return E_this.engineContain;
}
E_this.setEngineLevel=function(level){//设置引擎的档位
E_this.engineLevel=level;
}
E_this.getEngineLevel=function(){//获得引擎的档位
return E_this.engineLevel;
}
E_this.setEngineRunStatus=function(status){//设置引擎的运行状态
E_this.engineRunStatus=status;
}
E_this.getEngineRunStatus=function(){//获得引擎的运行状态
return E_this.engineRunStatus;
}
E_this.engineStart=function(){ //开启传送带方法
E_this.setEngineRunStatus(1);
if(E_this.engineClock==null||E_this.engineClock==undefined){
E_this.engineClock=setInterval(E_this.move,E_this.getEngineLevel());
document.getElementById("run_status").innerHTML="运行中......"
}else{
E_this.engineClock=clearInterval(E_this.engineClock);
E_this.engineClock=setInterval(E_this.move,E_this.getEngineLevel());
document.getElementById("run_status").innerHTML="运行中"
}
} E_this.move=function(){//让引擎传送带运行起来
var arrCon=E_this.getEngineContain();
//alert(arrCon.length);
for(var i=0;i<arrCon.length;i++){
if(arrCon.length>0){
var box=arrCon[i]; //定义一个盒子变量放入发动机盒子容器中
var nodeObj=box.getHtmlNodeObj();//定义一个接受盒子中label节点对象的变量
var topTemp=parseInt(nodeObj.style.top);//获得盒子中label节点对象的高度
if(topTemp>=E_this.engineLength){//判断盒子下降的高度是否越界
//alert(topTemp>=_this.engineLength);
arrCon.splice(i,1);//如果越界就从发动机盒子容器中移除该盒子
box.setIsExist(false);//并且让盒子消失
E_this.cores=E_this.cores-1;;//计分器-1
}else{
nodeObj.style.top=(topTemp+1)+"px";//盒子如果没有越界那么让盒子继续下降
//alert(nodeObj.style.top);
}
}
}
}
E_this.changeLevel=function(level){//调换难度
E_this.setEngineLevel(level);
E_this.engineClock=clearInterval(E_this.engineClock);
E_this.engineClock=setInterval(E_this.move(),level);
}
this.enginePause=function(){//暂停游戏
E_this.setEngineRunStatus(2);
}
this.engineStop=function(){//停止游戏
E_this.setEngineRunStatus(0);
}
this.compTransport=function(){//
var arrCon=getEngineContain();
for(var i=0;i<arrCon.length;i++){
arrCon.splice(i,1);
arrCon[i].setIsExist(false);
}
}
}function Observer(engine){
var O_this=this;
O_this.obsPower=1000;
O_this.obsTakePower=1000;
O_this.obsClock;
O_this.obsClockTake;
O_this.idBT=0;
//向发动机上放置盒子
O_this.engineObs=engine;
{
if(O_this.engineObs==null||O_this.engineObs==undefined){
alert("引擎参数未被初始化!");
}
}
O_this.getObsClock=function(){
return O_this.obsClock;
}
O_this.setObsPower=function(rate){
O_this.obsPower=rate;
}
O_this.getObsPower=function(){
return O_this.obsPower;
}
O_this.setIdBT=function(){
O_this.idBT=O_this.idBT+1;
}
O_this.getIdBT=function(){
return O_this.idBT;
}
O_this.setEngineObs=function(engineTemp){
O_this.engineObs=engineTemp;
}
O_this.getEngineObs=function(){
return O_this.engineObs;
}
O_this.takeBox=function(box){
var boxObs=box;
boxObs.setIsExist(false);
}
O_this.makeBoxName=function(){
var randomASC=65+parseInt(Math.random()*26);
return randomASC;
}
O_this.putBox=function(){
O_this.obsClock=setInterval(function(){
var eng=O_this.getEngineObs();//拿传送引擎
//alert("12345");
var engCon=eng.getEngineContain();
var x=parseInt(Math.random()*parseInt(eng.engineWidth));
//alert(x);
var box=new Box(O_this.getIdBT(),O_this.makeBoxName());//拿一个盒子
//var box=new Box();
// alert(box.getId());
box.setId();
box.htmlNodeObj.style.left=x;
engCon.push(box);
box.setIsExist(true);
},O_this.getObsPower());
}
}function makeWordFromKeyBoard(){
var wordId=event.keyCode;
alert(wordId);
var boxWord=new Box(wordId);
var engConArray=EngineTransport().getEngineContain
for(var i=0;i<engConArray.lenght;i++){
if(engConArray[i].bName=wordId){
engConArray[i].htmlNodeObj.style.display="none";
engConArray.splice(i,1);
}
}
}function startGameFromKeyBoard(){
var et=new EngineTransport();//购买一台传送引擎
et.engineStart();
et.changeLevel(80);
var observer=new Observer(et);//雇用一個管理人員或操作員
observer.setObsPower(1000);
observer.putBox();
}
代码如下:
HTMLCode:
<html>
<head>
<title>打字</title>
<link type="text/css" href="./css/objMain.css" rel="stylesheet">
<script src="./js/objClickWords.js"></script>
</head>
<body onkeypress="makeWordFromKeyBoard()">
<div id="outer">
<div id="inner_top">
<!--主显示区域即字母下降区域-->
</div>
<div id="inner_bottom">
<!--状态和控制面板区域-->
分数:<span id="cores">0</span>
難度:<select onchange="changeGameLevel()" id="hard">
<option value="700">7</option>
<option value="600">6</option>
<option value="500">5</option>
<option value="400">4</option>
<option value="300">3</option>
<option value="200">2</option>
<option value="100">1</option>
</select>
运行状态:<span id="run_status">停止中.......</span>
<button onclick="startGameFromKeyBoard()">begin</button>
<button onclick="pauseGame()">pause</button>
</div>
</div>
</body>
</html>
jsCode:
//盒子或容器
function Box(boxId,boxName){
var B_this=this;
B_this.id=boxId;
B_this.bName=boxName;
B_this.boxSize;//容器大小
B_this.isExist=false;//盒子是否在引擎上
B_this.htmlNodeObj;
{
//alert("999");
if(B_this.id==null||B_this.id==undefined){
alert("盒子参数未初始化1!");
return;
}
if(B_this.bName==null||B_this.bName==undefined){
alert("盒子参数未初始化2!");
return;
}
B_this.htmlNodeObj=document.createElement("label");
B_this.htmlNodeObj.id=B_this.id;
B_this.htmlNodeObj.innerHTML=String.fromCharCode(B_this.bName);
B_this.htmlNodeObj.style.position="absolute";
B_this.htmlNodeObj.style.top=0+"px";
}
B_this.getHtmlNodeObj=function(){//获得label节点对象
return B_this.htmlNodeObj;
}
B_this.setBName=function(bxName){//为盒子命名
B_this.bName=bxName;
}
B_this.getBName=function(){//获得盒子的名字
return B_this.bName;
}
B_this.setId=function(bId){//给盒子一个id
B_this.id=bId;
}
B_this.getId=function(){//获得盒子的id
return B_this.id;
}
B_this.getBoxSize=function(){//获得盒子的大小
return B_this.boxSize;
}
B_this.setBoxSize=function(size){//设置盒子的大小
B_this.boxSize=size;
}
B_this.setIsExist=function(isOrExist){//判断盒子是否存在
if(isOrExist){
document.getElementById("inner_top").appendChild(B_this.htmlNodeObj);
B_this.htmlNodeObj.style.display="block";
// alert("我进div了");
}else{
document.getElementById("inner_top").removeChild(B_this.htmlNodeObj);
}
}
B_this.getIsExist=function(){//获得盒子的状态
return B_this.isExist;
}
}
//传送引擎
function EngineTransport(){
var E_this=this;
E_this.cores=0;//计分器
E_this.engineContain=new Array();//引擎容器
E_this.engineLength=parseInt(document.getElementById("inner_top").offsetHeight);//传送引擎的传送长度
E_this.engineWidth=parseInt(document.getElementById("inner_top").offsetWidth);//传送引擎的宽度
E_this.engineLevel=3000;//传送引擎的档位,20>>40>>>60>>>>80
E_this.engineRunStatus=0;//传送引擎的运行状态:0--停止,1--开始,2--暂停.
E_this.engineClock;//传送引擎的传送速度
E_this.getEngineContain=function(){//获得引擎容器
return E_this.engineContain;
}
E_this.setEngineLevel=function(level){//设置引擎的档位
E_this.engineLevel=level;
}
E_this.getEngineLevel=function(){//获得引擎的档位
return E_this.engineLevel;
}
E_this.setEngineRunStatus=function(status){//设置引擎的运行状态
E_this.engineRunStatus=status;
}
E_this.getEngineRunStatus=function(){//获得引擎的运行状态
return E_this.engineRunStatus;
}
E_this.engineStart=function(){ //开启传送带方法
E_this.setEngineRunStatus(1);
if(E_this.engineClock==null||E_this.engineClock==undefined){
E_this.engineClock=setInterval(E_this.move,E_this.getEngineLevel());
document.getElementById("run_status").innerHTML="运行中......"
}else{
E_this.engineClock=clearInterval(E_this.engineClock);
E_this.engineClock=setInterval(E_this.move,E_this.getEngineLevel());
document.getElementById("run_status").innerHTML="运行中"
}
} E_this.move=function(){//让引擎传送带运行起来
var arrCon=E_this.getEngineContain();
//alert(arrCon.length);
for(var i=0;i<arrCon.length;i++){
if(arrCon.length>0){
var box=arrCon[i]; //定义一个盒子变量放入发动机盒子容器中
var nodeObj=box.getHtmlNodeObj();//定义一个接受盒子中label节点对象的变量
var topTemp=parseInt(nodeObj.style.top);//获得盒子中label节点对象的高度
if(topTemp>=E_this.engineLength){//判断盒子下降的高度是否越界
//alert(topTemp>=_this.engineLength);
arrCon.splice(i,1);//如果越界就从发动机盒子容器中移除该盒子
box.setIsExist(false);//并且让盒子消失
E_this.cores=E_this.cores-1;;//计分器-1
}else{
nodeObj.style.top=(topTemp+1)+"px";//盒子如果没有越界那么让盒子继续下降
//alert(nodeObj.style.top);
}
}
}
}
E_this.changeLevel=function(level){//调换难度
E_this.setEngineLevel(level);
E_this.engineClock=clearInterval(E_this.engineClock);
E_this.engineClock=setInterval(E_this.move(),level);
}
this.enginePause=function(){//暂停游戏
E_this.setEngineRunStatus(2);
}
this.engineStop=function(){//停止游戏
E_this.setEngineRunStatus(0);
}
this.compTransport=function(){//
var arrCon=getEngineContain();
for(var i=0;i<arrCon.length;i++){
arrCon.splice(i,1);
arrCon[i].setIsExist(false);
}
}
}function Observer(engine){
var O_this=this;
O_this.obsPower=1000;
O_this.obsTakePower=1000;
O_this.obsClock;
O_this.obsClockTake;
O_this.idBT=0;
//向发动机上放置盒子
O_this.engineObs=engine;
{
if(O_this.engineObs==null||O_this.engineObs==undefined){
alert("引擎参数未被初始化!");
}
}
O_this.getObsClock=function(){
return O_this.obsClock;
}
O_this.setObsPower=function(rate){
O_this.obsPower=rate;
}
O_this.getObsPower=function(){
return O_this.obsPower;
}
O_this.setIdBT=function(){
O_this.idBT=O_this.idBT+1;
}
O_this.getIdBT=function(){
return O_this.idBT;
}
O_this.setEngineObs=function(engineTemp){
O_this.engineObs=engineTemp;
}
O_this.getEngineObs=function(){
return O_this.engineObs;
}
O_this.takeBox=function(box){
var boxObs=box;
boxObs.setIsExist(false);
}
O_this.makeBoxName=function(){
var randomASC=65+parseInt(Math.random()*26);
return randomASC;
}
O_this.putBox=function(){
O_this.obsClock=setInterval(function(){
var eng=O_this.getEngineObs();//拿传送引擎
//alert("12345");
var engCon=eng.getEngineContain();
var x=parseInt(Math.random()*parseInt(eng.engineWidth));
//alert(x);
var box=new Box(O_this.getIdBT(),O_this.makeBoxName());//拿一个盒子
//var box=new Box();
// alert(box.getId());
box.setId();
box.htmlNodeObj.style.left=x;
engCon.push(box);
box.setIsExist(true);
},O_this.getObsPower());
}
}function makeWordFromKeyBoard(){
var wordId=event.keyCode;
alert(wordId);
var boxWord=new Box(wordId);
var engConArray=EngineTransport().getEngineContain
for(var i=0;i<engConArray.lenght;i++){
if(engConArray[i].bName=wordId){
engConArray[i].htmlNodeObj.style.display="none";
engConArray.splice(i,1);
}
}
}function startGameFromKeyBoard(){
var et=new EngineTransport();//购买一台传送引擎
et.engineStart();
et.changeLevel(80);
var observer=new Observer(et);//雇用一個管理人員或操作員
observer.setObsPower(1000);
observer.putBox();
}
E_this.setEngineLevel(level);
E_this.engineClock=clearInterval(E_this.engineClock);
E_this.engineClock=setInterval(E_this.move(),level);
}
里面的第一个E_this.setEngineLevel(level);如果用E_this.engineClock来接收那么这个时钟就不执行了 这是怎么回事?
这个clear的,只是一个表达式,没有返回值的吧
你直接写clearInterval(E_this.engineClock);
试试看