【Cocos03】runAction顺序和并行执行一系列动作

sequence 顺序执行

// 顺序执行两个动作
node.runAction(
    cc.sequence(
        cc.moveTo(),
        cc.rotateTo(),
        cc.callFunc(() => {})
    );
)

spawn 并行执行

// 并行执行两个动作
node.runAction(
    cc.spawn(
        cc.moveTo(),
        cc.rotateTo(),
        cc.callFunc(() => {})
    );
)

先定义好动作再执行

buttonShake(node) {
    const actionLeft = cc.moveBy(0.1, cc.v2(-5, 0));
    const actionRight = cc.moveBy(0.1, cc.v2(10, 0));
    const actionLeftSecond = cc.moveBy(0.1, cc.v2(-10, 0));
    const actionRightSecond = cc.moveBy(0.1, cc.v2(5, 0));
    return new Promise(resolve => {
        node.runAction(
            cc.sequence(actionLeft, actionRight, actionLeftSecond, actionRightSecond,
            // 执行动作完成之后调用的方法
                cc.callFunc(() => {
                    cc.log(3333);
                    resolve();
                }))
        );
    });
},

在runAction里定义动作

this.left.runAction(
        cc.sequence(
            cc.fadeIn(0.6),
            cc.callFunc(() => {
                this.buttonShake(event.target).then(() => {
                    cc.log(44444);
                    this.scheduleOnce(() => {
                        cc.log(2222222);
                        this.buttonShake(event.target);
                    }, 0.8);
                });
                this.playAudio(this.rightAudio).then(() => {
                    this.xxxx();
                });
            })
        )
    );

spawn也可以先定义好,然后在sequence里运行

const car1 = new Promise(resolve => {
            const spawn1 = cc.spawn(cc.moveTo(0.5, -196, 81), cc.rotateTo(0.5, 45));
            const spawn2 = cc.spawn(cc.moveTo(0.5, 72, 220), cc.rotateTo(0.5, 0));
            const move3 = cc.moveTo(0.5, 431, 227);
            const move4 = cc.moveTo(0.6, 1300, 220);
            this.car1.runAction(
                cc.sequence(
                    spawn1,
                    spawn2,
                    move3,
                    move4,
                    cc.callFunc(() => {
                        resolve();
                    })
                )
            );
        });

你可能感兴趣的:(cocos,前端)