op_pic.js 4.38 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: {
        // 图片
        pic: {
            default: null,
            type: cc.Sprite,
        },

wangxin's avatar
wangxin committed
15 16
        // 图片选中底图
        pic_selected: {
wangxin's avatar
wangxin committed
17
            default: null,
wangxin's avatar
wangxin committed
18
            type: cc.Node,
wangxin's avatar
wangxin committed
19 20 21 22 23 24 25 26
        },

        // 声音,0-正确,1-错误
        sounds: {
            default: [],
            type: [cc.AudioClip],
        },

wangxin's avatar
wangxin committed
27
        _opId: 0, // 选项id
wangxin's avatar
wangxin committed
28
        _ori_pos: null, // 初始位置
wangxin's avatar
wangxin committed
29 30 31
        _isEnter: false, // 是否碰到对的选框
        _isContact: false, // 是否碰到了选项框
        _soundOp: null, // 碰到的选框
wangxin's avatar
wangxin committed
32 33
        _picUrl: null, // 图片地址
        _spriteFrame: null,
wangxin's avatar
wangxin committed
34 35
        _width: null,
        _height: null,
wangxin's avatar
wangxin committed
36
        _isMoved: false,
wangxin's avatar
wangxin committed
37 38 39 40 41 42 43 44
    },

    onEnable() {
        cc.game.on("SELECT", this.selected, this);
    },

    onDisable() {
        cc.game.off("SELECT", this.selected, this);
wangxin's avatar
wangxin committed
45 46 47
    },

    onLoad() {
wangxin's avatar
wangxin committed
48 49 50
        this.scheduleOnce(function () {
            this._ori_pos = this.node.getPosition();
        }, 0.5);
wangxin's avatar
wangxin committed
51

wangxin's avatar
wangxin committed
52
        this.node.on("touchstart", this.onTouchStart, this);
wangxin's avatar
wangxin committed
53 54 55
        this.node.on("touchmove", this.onTouchMove, this);
        this.node.on("touchend", this.onTouchEnd, this);
        this.node.on("touchcancel", this.onTouchEnd, this);
wangxin's avatar
wangxin committed
56 57
    },

wangxin's avatar
wangxin committed
58 59
    setPic() {
        cc.loader.load({ url: this._picUrl }, (err, img) => {
wangxin's avatar
wangxin committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
            let frame = new cc.SpriteFrame(img);
            let frameSize = frame.getOriginalSize();
            this._spriteFrame = frame;

            let scale = 1;
            if (frameSize.height > 168) {
                scale = 168 / frameSize.height;
            }

            this.pic.spriteFrame = frame;
            this.pic.node.width = frameSize.width * scale;
            this.pic.node.height = frameSize.height * scale;

            scale = 1;
            if (frameSize.width > 168) {
                scale = 168 / frameSize.width;
            }
            this.pic.node.width = frameSize.width * scale;
            this.pic.node.height = frameSize.height * scale;

            this._width = this.pic.node.width;
            this._height = this.pic.node.height;
wangxin's avatar
wangxin committed
82 83
        });
    },
wangxin's avatar
wangxin committed
84

wangxin's avatar
wangxin committed
85 86 87 88 89
    onTouchStart(event) {
        this.pic_selected.active = true;
        cc.game.emit("SELECT", this._opId);
    },

wangxin's avatar
wangxin committed
90
    //触摸移动;
wangxin's avatar
wangxin committed
91 92 93 94
    onTouchMove(touch, event) {
        let delta = touch.getDelta();
        this.node.x += delta.x;
        this.node.y += delta.y;
wangxin's avatar
wangxin committed
95 96

        this._isMoved = true;
wangxin's avatar
wangxin committed
97 98
    },
    onTouchEnd() {
wangxin's avatar
wangxin committed
99
        if (this._isContact && !this._soundOp.node.getComponent("op_sound")._isRight) {
wangxin's avatar
wangxin committed
100 101
            if (this._isEnter) {
                console.log("enter right");
wangxin's avatar
wangxin committed
102
                this.playSound(0);
wangxin's avatar
wangxin committed
103

wangxin's avatar
wangxin committed
104
                this._soundOp.node.getComponent("op_sound").correct(this._spriteFrame, this._width, this._height);
wangxin's avatar
wangxin committed
105
                this.node.destroy();
wangxin's avatar
wangxin committed
106 107
            } else {
                console.log("enter false");
wangxin's avatar
wangxin committed
108
                this.playSound(1);
wangxin's avatar
wangxin committed
109
                this.node.active = false;
wangxin's avatar
wangxin committed
110
                this._soundOp.node.getComponent("op_sound").wrong(this._spriteFrame, this._width, this._height);
wangxin's avatar
wangxin committed
111
            }
wangxin's avatar
wangxin committed
112 113 114
        } else {
            this.node.setPosition(this._ori_pos);
        }
wangxin's avatar
wangxin committed
115 116 117 118 119

        if (this._isMoved) {
            this._isMoved = false;
            this.pic_selected.active = false;
        }
wangxin's avatar
wangxin committed
120 121 122 123 124 125 126 127 128
    },

    /**
     * 当碰撞产生的时候调用
     * @param  {Collider} other 产生碰撞的另一个碰撞组件
     * @param  {Collider} self  产生碰撞的自身的碰撞组件
     */
    onCollisionEnter: function (other, self) {
        console.log("on collision");
wangxin's avatar
wangxin committed
129 130
        this._soundOp = other;
        this._isContact = true;
wangxin's avatar
wangxin committed
131 132 133 134 135 136 137 138 139 140 141 142 143
        if (other.getComponent("op_sound")._opId == this._opId) {
            this._isEnter = true;
        }
    },

    /**
     * 当碰撞结束后调用
     * @param  {Collider} other 产生碰撞的另一个碰撞组件
     * @param  {Collider} self  产生碰撞的自身的碰撞组件
     */
    onCollisionExit: function (other, self) {
        console.log("on collision exit");
        this._isEnter = false;
wangxin's avatar
wangxin committed
144
        this._isContact = false;
wangxin's avatar
wangxin committed
145
    },
wangxin's avatar
wangxin committed
146 147 148 149 150

    // 播放声音
    playSound(index) {
        cc.audioEngine.playEffect(this.sounds[index], false);
    },
wangxin's avatar
wangxin committed
151 152 153 154 155 156 157 158 159 160 161

    resetPos() {
        this.node.setPosition(this._ori_pos);
    },

    selected(id) {
        if (this._opId == id) {
            return;
        }
        this.pic_selected.active = false;
    },
wangxin's avatar
wangxin committed
162
});