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) }
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配