javascript - JavaScript 中的 Math.abs

标签 javascript jquery html canvas

我对 math.abs() 感到困惑。 我通过互联网做了研究,但找不到与这个弹跳球的动画有任何关系。我想知道这是如何工作的,以及在使用 math.abs() 函数后球是如何平稳弹跳的?

function bounce() {
    if (x + dx > 293 || x + dx < 0) {
        dx = -dx;
    }
    if (y >= 290) {
        y = 290;
    }
    if (y + dy > 290 || y + dy < 0) {
        dx *= 0.99;
        dy = -dy;
    }
    //if (Math.abs(dx) < 0.01) {
       // dx = 0;
    }
    dy++;
}

我确实评论 这条线让我感到困惑。任何人都请告诉我这个功能对于这个动画有多重要。

Fiddle

最佳答案

dx 是 x 上的位移。

Math.abs(dx) 是 x 上的绝对速度,即不带符号的值,始终为正或空。

if (Math.abs(dx) < 0.01) {

可以写成

if (dx>-0.01 && dx < 0.01) {

基本上,如果球已经很慢,这条线和下面那条线会沿着 x 停止球。

关于javascript - JavaScript 中的 Math.abs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14750182/

相关文章:

jquery - 表格后的文本(不适合放在 div 中)出现在表格中

html - 如何使用 CSS 获取两个 div 类实例中的第二个?

javascript - CSS 类仅在验证时工作一次

javascript - React App 中 API 的用户身份验证

javascript - 母版页 url 作为前缀附加到 sharepoint 2013 中的 anchor href

javascript - 如果其他元素存在,则 jQuery 切换元素

jquery - 我想格式化我的时间选择器 jquery

javascript - 使用 Auth 在新选项卡中打开 PDF 文档

javascript - Number 作为默认的 jquery 插件选项

javascript - 如何在两个日期之间定义剑道网格列过滤器?