Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何让球挂在绳子上?_Javascript_Canvas_P5.js - Fatal编程技术网

Javascript 如何让球挂在绳子上?

Javascript 如何让球挂在绳子上?,javascript,canvas,p5.js,Javascript,Canvas,P5.js,我正在尝试使用P5.js库制作一个简单的游戏,其中一个球用绳子固定在一个大头针上,当球落下时,它不能超过绳子的长度。 球必须落下,直到它与销的距离等于绳子的长度。我怎么做?我只需要它和y轴一起工作 下面是一些代码: var化合物; 功能化合物{ 这是指直径=25; 此值为0.pinx=宽度/2; 这一点。piny=高度/2; 这个球直径=50; 此值为0.x=宽度/2; 此.bally=高度/2+200; 该宽度=4; 该高度=200; this.ropex=this.pinx-this.ro

我正在尝试使用P5.js库制作一个简单的游戏,其中一个球用绳子固定在一个大头针上,当球落下时,它不能超过绳子的长度。 球必须落下,直到它与销的距离等于绳子的长度。我怎么做?我只需要它和y轴一起工作

下面是一些代码:

var化合物; 功能化合物{ 这是指直径=25; 此值为0.pinx=宽度/2; 这一点。piny=高度/2; 这个球直径=50; 此值为0.x=宽度/2; 此.bally=高度/2+200; 该宽度=4; 该高度=200; this.ropex=this.pinx-this.ropeWidth/2; this.ropey=this.piny; this.updatePin=函数{ } this.updateBall=函数{ this.ballSpeed=this.ballSpeed+1; this.bally=this.bally+this.ballSpeed; } this.updateRope=函数{ } this.show=函数{ ellipsethis.pinx,this.piny,this.pinDiameter; 填充255; 仰泳; ellipsethis.ballx,this.bally,this.ballDiameter; 填充255; 仰泳; recthis.ropex,this.ropey,this.ropeWidth,this.ropeHeight; 填充255; 仰泳; } } 功能设置{ CreateCanvas600600; 化合物=新化合物; } 函数图{ 背景0; 复方阿替平 composite.updateBall composite.updateRope 复合表演 } 希望能有帮助

var化合物; 功能化合物{ 这是指直径=25; 此值为0.pinx=宽度/2; 这一点。piny=高度/2; 这个球直径=50; this.ballx=this.pinx; this.bally=this.piny+100; 此值为0.ballSpeed=0; 这个重力=0.5; 该宽度=4; 该高度=200; this.ropex=this.pinx-this.ropeWidth/2; this.ropey=this.piny; this.onPin=函数x,y{ 设dx=x—this.pinx; 让dy=y-这个.piny; 设dist=Math.sqrtdx*dx,dy*dy 返回距离此。piny+200{ this.bally=this.piny+200; 此值为0.ballSpeed=0; } } this.updateRope=函数{ 如果是这个,拖动{ this.ropey=this.piny; this.ropehight=this.ropehight计算; } } this.show=函数{ ellipsethis.pinx,this.piny,this.pinDiameter; 填充255; 仰泳; ellipsethis.ballx,this.bally,this.ballDiameter; 填充255; 仰泳; recthis.ropex,this.ropey,this.ropeWidth,this.ropeHeight; 填充255; 仰泳; } } 功能鼠标按下{ 如果是component.onPinmouseX,mouseY 复合捕获; } 功能鼠标{ 复合液滴; } 功能设置{ CreateCanvas600600; 化合物=新化合物; } 函数图{ 背景0; 复方阿替平 composite.updateBall composite.updateRope 复合表演 }
从OP代码开始,这里有一个示例,它允许球沿着绳子向下滑动,就像绳子穿过球一样,并且在末端有一个结

var化合物; 功能化合物{ 这是指直径=25; 此值为0.pinx=宽度/2; this.piny=this.pinDiameter; 这个球直径=50; 此值为0.x=宽度/2; this.bally=this.ballDiameter+this.pinDiameter*2; 这是1.ballSpeed=1; 该宽度=4; 该高度=200; this.ropex=this.pinx-this.ropeWidth/2; this.ropey=this.piny; this.updatePin=函数{ } this.updateBall=函数{ 如果this.bally我建议对距离函数中的逻辑进行代码更改,因为它看起来像是一个打字错误。我也要留下我的答案,因为它显示了这个问题所要求的行为,而且附加的内容更少。移动pin是一种很好的方式,但可能与问题没有直接关系。