p5.j​​s中的弹跳球未激活条件语句

时间:2019-01-09 04:04:06

标签: javascript p5.js

我对JavaScript和p5.js完全陌生。我知道这不是弹跳球的最有效代码,但是我尝试编写自己的弹跳球机制作为自学方法。

function draw() {
    background (col); // background
    col = map(cir.x, 0, 600, 0, 255) // map function

    fill (350, 200, 200);
    ellipse (cir.x, cir.y, cir.diameter, cir.diameter);

    // Bounding ball mechanic
    var speed = 3;
    var hitRightWall = false;
    var hitLeftWall = false;

    if (cir.x >= 300) {
        hitRightWall == true;
        hitLeftWall == false;
    }
    else if (cir.x <= -50) {
        hitLeftWall == true;
        hitRightWall == false;
    }
    if (hitRightWall==true) {
        speed == -3;
    }
    else {
        speed == 3;
    }
    cir.x = cir.x + speed;
}

由于某种原因,即使激活了if (hitRightWall==True),也从未激活if (cir.x >= 300)条件。球一直向右移动并离开屏幕。

1 个答案:

答案 0 :(得分:1)

这里发生了一些事情。首先,您将每帧将hitRightWallhitLeftWall的值重置为false。那是你想要的吗?

第二,看看这样的语句:

hitRightWall == true;

==双重等于是在做比较,而不是赋值。您可能想要这样的东西:

hitRightWall = true;

=单个等于将值分配给变量。

最后,我不太确定为什么根本不需要这些变量。您不能直接在第一个speed语句中直接修改if / else if变量吗?

无耻的自我促进:here是有关冲突检测的教程。它是为处理而编写的,但是相同的想法也适用于P5.js。