const { ccclass, property } = cc._decorator;

@ccclass
export default class Test extends cc.Component {

    private wid2 = 1136 / 2;
    private r: number = 32;
    private left: number = -this.wid2 + this.r;
    private right: number = this.wid2 - this.r;
    private v0: number = 10;
    private v: number;

    private ball: cc.Node;

    protected start(): void {
        this.ball = this.node.getChildByName("ball");
        this.v = this.v0;
    }

    protected update(): void {
        this.ball.x += this.v;
        if (this.v < 0) {
            if (this.ball.x <= this.left) {
                this.ball.x = this.left;
                this.v = this.v0;
            }
        } else {
            if (this.ball.x >= this.right) {
                this.ball.x = this.right;
                this.v = -this.v0;
            }
        }
    }

}
