Laya贝塞尔运动(金币获取运动)

一,贝塞尔运动类BezierPath.js是 一个用来生成 一系列 贝塞尔 曲线上面的点的 工具类。 换句话说我只要把我的金币的位置按照生成的定点位移就能实现 ,金币的移动轨迹就是贝塞尔曲线了

class BezierPath{
    static CreateBezierPoints(anchorpoints, pointsAmount) {
        var points = [];
        for (var i = 0; i < pointsAmount; i++) {
            var point = this.MultiPointBezier(anchorpoints, i / pointsAmount);
            points.push(point);
        }
        return points;
    }
 
    static MultiPointBezier(points, t) {
        let len = points.length;
        let x = 0, y = 0;
        for (let i = 0; i < len; i++) {
            let point = points[i];
            x += point.x * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (this.erxiangshi(len - 1, i));
            y += point.y * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (this.erxiangshi(len - 1, i));
        }
        return { x: x, y: y };
    }
 
    static erxiangshi(start, end) {
            let cs = 1, bcs = 1;
            while (end > 0) {
                cs *= start;
                bcs *= end;
                start--;
                end--;
            }
            return (cs / bcs);
     }
}

二,根据BezierPath.js生成的点遍历数组

 onLog(){
            let logo = this.owner;
            let points = []
            let point1 = new Laya.Point(logo.x, logo.y) // 起点
            console.log(point1)
            let point2 = new Laya.Point(Math.random()*300, Math.random()*300)//一个顶点 还可以继续添加 点
            //let point3 = new Laya.Point(600, 100)//一个顶点 还可以继续添加 点
            let point4 = new Laya.Point(0, 0)//终点
     
            points.push(point1)
            points.push(point2)
            //points.push(point3)
            points.push(point4)
            let array = BezierPath.CreateBezierPoints(points, 60)
     
            let index = 0
            //Laya.timer.loop()
            Laya.timer.loop(10, this, function dsq() {
                if (index > array.length - 1){
                    Laya.timer.clear(this,dsq)
                }else{
                    //index = 0
                logo.pos(array[index].x, array[index].y)
                index++
                }
            })
        }

三,制作预制体添加到舞台

 onEnable() {
        for(var i =0; i<50; i++){
            this.addGold();
        }
    }

    addGold(){
        var m = this.gold_fb.create();
        m.pos(750*Math.random(),1300*Math.random());
        this.owner.addChild(m);
    }

参考
https://blog.csdn.net/nicepainkiller/article/details/91390452

你可能感兴趣的:(Laya贝塞尔运动(金币获取运动))