年年月月日日无bug 2023-04-01 13:43 采纳率: 54.5%
浏览 39
已结题

js setinterval与this

<div id = 'box'>
<script> var foods = false var a = document.getElementById('box') function Snake(){ this.bod = [{x:4,y:0},{x:3,y:0},{x:2,y:0},{x:1,y:0}] this.way = 'right' this.init = function(){ for(var i=0;i<this.bod.length;i++){ var s = document.createElement('div') s.style.width = 20 + 'px' s.style.height = 20 + 'px' s.style.position = 'absolute' s.style.background = 'red' s.style.left = this.bod[i].x*20 + 'px' s.style.top = this.bod[i].y*20 + 'px' this.bod[i].f = s a.appendChild(s) } } this.move = function(){ for(var i=this.bod.length-1;i>0;i--){ this.bod[i].x = this.bod[i-1].x this.bod[i].y = this.bod[i-1].y } switch(this.way) { case 'right': this.bod[0].x += 1 break case 'left': this.bod[0].x -= 1 break case 'down': this.bod[0].y += 1 break case 'up': this.bod[0].y -= 1 break } for(var j=0;j<this.bod.length;j++){ if(this.bod[j].f != null){ a.removeChild(this.bod[j].f) } } this.init() if( (this.bod[0].x*20) >900 || this.bod[0].x<0 ){ alert("你出界了") clearInterval(ii); } if( (this.bod[0].y*20)>500 || this.bod[0].y<0){ alert("你出界了") clearInterval(ii); } for(var o=4;o<this.bod.length;o++){ if(this.bod[o].f != null){ if(this.bod[0].x==this.bod[o].x&&this.bod[0].y==this.bod[o].y){ clearInterval(ii); alert("大聪明,你怎么能吃自己呢"); } } } } } function food(){ this.init = function(){ this.x = Math.floor(Math.random()*45)*20 this.y = Math.floor(Math.random()*25)*20 var t = document.createElement('div') this.node = t t.style.width = 20 + 'px' t.style.height = 20 + 'px' t.style.left = this.x + 'px' t.style.top = this.y + 'px' t.style.position = 'absolute'; t.style.background = 'blue'; a.appendChild(t) } this.init() } function c(){ if((aa.bod[0].x*20) == ds.x&&(aa.bod[0].y*20) == ds.y){ if(ds.node != null){ a.removeChild(ds.node); aa.bod.push({x:null,y:null,f:null}); ds.init(); } } } var c_s = setInterval('c()',1) var ds = new food() var aa = new Snake() var ii = setInterval('aa.move()',200) 问题出在这 document.onkeydown = k function k(e){ var e = event || window.event switch(e.keyCode){ case 38: if(aa.way){ aa.way = 'up' } break case 40: if(aa.way){ aa.way = 'down' } break case 37: if(aa.way){ aa.way = 'left' } break case 39: if(aa.way){ aa.way = 'right' } break } }

这样行

<div id = 'box'>
<script> var foods = false var a = document.getElementById('box') function Snake(){ this.bod = [{x:4,y:0},{x:3,y:0},{x:2,y:0},{x:1,y:0}] this.way = 'right' this.init = function(){ for(var i=0;i<this.bod.length;i++){ var s = document.createElement('div') s.style.width = 20 + 'px' s.style.height = 20 + 'px' s.style.position = 'absolute' s.style.background = 'red' s.style.left = this.bod[i].x*20 + 'px' s.style.top = this.bod[i].y*20 + 'px' this.bod[i].f = s a.appendChild(s) } } this.move = function(){ for(var i=this.bod.length-1;i>0;i--){ this.bod[i].x = this.bod[i-1].x this.bod[i].y = this.bod[i-1].y } switch(this.way) { case 'right': this.bod[0].x += 1 break case 'left': this.bod[0].x -= 1 break case 'down': this.bod[0].y += 1 break case 'up': this.bod[0].y -= 1 break } for(var j=0;j<this.bod.length;j++){ if(this.bod[j].f != null){ a.removeChild(this.bod[j].f) } } this.init() if( (this.bod[0].x*20) >900 || this.bod[0].x<0 ){ alert("你出界了") clearInterval(ii); } if( (this.bod[0].y*20)>500 || this.bod[0].y<0){ alert("你出界了") clearInterval(ii); } for(var o=4;o<this.bod.length;o++){ if(this.bod[o].f != null){ if(this.bod[0].x==this.bod[o].x&&this.bod[0].y==this.bod[o].y){ clearInterval(ii); alert("大聪明,你怎么能吃自己呢"); } } } } } function food(){ this.init = function(){ this.x = Math.floor(Math.random()*45)*20 this.y = Math.floor(Math.random()*25)*20 var t = document.createElement('div') this.node = t t.style.width = 20 + 'px' t.style.height = 20 + 'px' t.style.left = this.x + 'px' t.style.top = this.y + 'px' t.style.position = 'absolute'; t.style.background = 'blue'; a.appendChild(t) } this.init() } function c(){ if((aa.bod[0].x*20) == ds.x&&(aa.bod[0].y*20) == ds.y){ if(ds.node != null){ a.removeChild(ds.node); aa.bod.push({x:null,y:null,f:null}); ds.init(); } } } var c_s = setInterval('c()',1) var ds = new food() var aa = new Snake() var ii = setInterval(aa.move(),200)问题出在这 document.onkeydown = k function k(e){ var e = event || window.event switch(e.keyCode){ case 38: if(aa.way){ aa.way = 'up' } break case 40: if(aa.way){ aa.way = 'down' } break case 37: if(aa.way){ aa.way = 'left' } break case 39: if(aa.way){ aa.way = 'right' } break } }

这样却不行

  • 写回答

2条回答 默认 最新

  • 关注

    你加单引号,它是定时执行你字符串的命令,你不加单引号,它就调用一次方法就没了,就成了一个普通方法调用了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月17日
  • 已采纳回答 4月9日
  • 创建了问题 4月1日

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢