qq_36896043 2018-05-29 08:22 采纳率: 14.3%
浏览 1071
已结题

js里面的碰撞检测。。。。。。。。。

这一段代码里,我想要方块在堆积到顶端以后就会停止下落(希望在原代码上修改)

  • 写回答

6条回答 默认 最新

  • qq_36896043 2018-05-29 08:23
    关注
    var cvs=document.getElementById("canvas") var cxt=cvs.getContext("2d") var gravity=0.5 var colors=new Array("#00BFFF","#FFFF00","#FF8C00","#AB82FF","#008B8B") var rects=new Array() var temp=creatRect() window.requestAnimationFrame(drawFrame,cvs) function drawFrame(){ window.requestAnimationFrame(drawFrame,cvs) cxt.clearRect(0,0,cvs.width,cvs.height) temp.vy+=gravity temp.y+=temp.vy if(temp.y+temp.height>cvs.height){ temp.y=cvs.height-temp.height temp=creatRect() } for(var i=0;i<rects.length;i++){ drawRect(rects[i]) } } function creatRect(){ var temp=new Rect() temp.lineWidth=1 temp.color=colors[Math.round(Math.random()*(colors.length-1))] temp.width=Math.round(Math.random()*30+20) temp.height=Math.round(Math.random()*30+20) temp.x=Math.round(Math.random()*(cvs.width-temp.width)) temp.y=0 rects.push(temp) return temp } function drawRect(rect){ if(temp!=rect&&hitTest(temp,rect)){ temp.y=rect.y-temp.height temp=creatRect() } rect.draw(cxt) } function hitTest(rectA,rectB){ return!(rectA.x+rectA.width<rectB.x||rectB.x+rectB.width<rectA.x||rectA.y+rectA.height<rectB.y||rectB.y+rectB.height<rectA.y) }
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!