Typescript来回移动对象

时间:2018-03-15 18:57:01

标签: typescript object position

我正在制作打字稿网页游戏。

我需要物体沿Y轴来回移动。 enter image description here

所以基本上,对象从 coorY 开始,速度为 dy ,其当前坐标为 Y

当前 Y 小于 coorY (10)时,当它大于 coorY 时,它向右移动 50 (60),向左移动。

public Move(): void {
        this._dy = 1;
        this.dir = true;
        if (this.y > this.coorY + 50) {
            this.dir = true;
            console.log("Forth " + (this.y - this.coorY));
        }
        else if (this.y < this.coorY) {
            this.dir = false;
            console.log("Back " + (this.y - this.coorY));
        }

        if (this.dir) {
            this.y -= this._dy;
        }
        else if (!this.dir) {
            this.y += this._dy;
        } 
    }

但不知何故,物体移动很少,看起来像是在颤抖,或者只是停留在原来的位置。如何让它来回移动?

1 个答案:

答案 0 :(得分:1)

从移动功能中删除this.dir = true;

Example