【问题标题】:Typescript move object back and forth打字稿来回移动对象
【发布时间】:2018-03-15 18:57:01
【问题描述】:

我正在开发一个打字稿网页游戏。

我需要对象沿 Y 轴来回移动。

所以基本上,物体从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;
        } 
    }

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

【问题讨论】:

    标签: typescript object position


    【解决方案1】:

    从 Move 函数中移除 this.dir = true;

    Example

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 2019-07-24
      • 2017-06-12
      • 2017-02-18
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2015-03-24
      相关资源
      最近更新 更多