0

I am working on a typescript web game.

I need the object to move back and forth along Y-Axis. enter image description here

So basically, the object starts at coorY, with speed of dy, its current coordinate is Y.

When current Y is less than coorY(10), it move towards right, when it is greater than coorY + 50 (60), it move towards left.

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;
        } 
    }

But somehow the object moves very little, it looks like it is shaking, or just stuck at original position. How do I make it move back and forth?

1 Answer 1

1

Remove this.dir = true; from inside the Move function.

Example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.