op_sound.js 2.96 KB
Newer Older
wangxin's avatar
wangxin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * 选项框(带播放按钮)
 */

cc.Class({
    extends: cc.Component,

    properties: {
        // 声音图标
        iconSound: {
            default: null,
            type: cc.Node,
        },

wangxin's avatar
wangxin committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
        // 声音图标
        rightFrame: {
            default: null,
            type: cc.Node,
        },

        // 声音图标
        rightPic: {
            default: null,
            type: cc.Sprite,
        },

        // 星星特效
        particle_star: {
            default: null,
            type: cc.Prefab,
        },

wangxin's avatar
wangxin committed
33 34
        _opId: 0, // 选项id
        _soundUrl: "", // 音频文件地址
wangxin's avatar
wangxin committed
35 36
        _opPic: null,
        _isRight: false,
wangxin's avatar
wangxin committed
37 38 39 40 41 42 43 44 45
    },

    /**
     * 播放声音
     */
    play() {
        if (this._soundUrl != "") {
            cc.assetManager.loadRemote(this._soundUrl, (err, audioClip) => {
                const audioId = cc.audioEngine.play(audioClip, false, 0.8);
wangxin's avatar
wangxin committed
46
                this.iconSound.getComponent(cc.Animation).play("icon_sound_play");
wangxin's avatar
wangxin committed
47
                cc.audioEngine.setFinishCallback(audioId, () => {
wangxin's avatar
wangxin committed
48
                    this.iconSound.getComponent(cc.Animation).play("icon_sound_finish");
wangxin's avatar
wangxin committed
49 50 51 52
                });
            });
        }
    },
wangxin's avatar
wangxin committed
53 54 55 56 57 58

    /**
     * 当碰撞产生的时候调用
     * @param  {Collider} other 产生碰撞的另一个碰撞组件
     * @param  {Collider} self  产生碰撞的自身的碰撞组件
     */
wangxin's avatar
wangxin committed
59 60 61
    onCollisionEnter: function (other, self) {
        this._opPic = other;
    },
wangxin's avatar
wangxin committed
62 63 64 65 66 67 68 69 70

    /**
     * 当碰撞结束后调用
     * @param  {Collider} other 产生碰撞的另一个碰撞组件
     * @param  {Collider} self  产生碰撞的自身的碰撞组件
     */
    onCollisionExit: function (other, self) {
        console.log("on collision exit");
    },
wangxin's avatar
wangxin committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

    correct(spriteFrame) {
        this.rightFrame.active = true;
        // cc.loader.load({ url }, (err, img) => {
        //     const spriteFrame = new cc.SpriteFrame(img);
        //     this.rightPic.spriteFrame = spriteFrame;
        // });
        this.rightPic.spriteFrame = spriteFrame;

        var par = cc.instantiate(this.particle_star);
        this.node.addChild(par);

        this._isRight = true;

        cc.game.emit("RIGHT");
    },

    wrong(spriteFrame) {
        this.rightFrame.active = true;
        // cc.loader.load({ url }, (err, img) => {
        //     const spriteFrame = new cc.SpriteFrame(img);
        //     this.rightPic.spriteFrame = spriteFrame;
        // });
        this.rightPic.spriteFrame = spriteFrame;
        let time = 0.1;
        let action = cc.repeat(
            cc.sequence(cc.moveBy(time, -10, 0), cc.moveBy(time, 10, 0), cc.moveBy(time, 10, 0), cc.moveBy(time, -10, 0)),
            5
        );

        this.node.runAction(action);
        this.scheduleOnce(function () {
            this.rightFrame.active = false;
            this.rightPic.spriteFrame = null;
            this._opPic.node.active = true;
            this._opPic.node.getComponent("op_pic").resetPos();
        }, 2.2);
    },
wangxin's avatar
wangxin committed
109
});