op_pic.js 3.97 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 17 18 19 20 21 22 23 24 25 26
        // 星星特效
        particle_star: {
            default: null,
            type: cc.Prefab,
        },

        // 声音,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 34 35 36 37 38 39
    },

    onLoad() {
        this.nodePos = this.node.getPosition();

        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
40 41 42 43 44 45 46 47
    },

    setPic(url) {
        cc.loader.load({ url }, (err, img) => {
            const spriteFrame = new cc.SpriteFrame(img);
            this.pic.spriteFrame = spriteFrame;
        });
    },
wangxin's avatar
wangxin committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

    //触摸移动;
    onTouchMove(event) {
        var self = this;
        var touches = event.getTouches();
        //触摸刚开始的位置
        var oldPos = self.node.convertToNodeSpaceAR(touches[0].getStartLocation());
        //触摸时不断变更的位置
        var newPos = self.node.convertToNodeSpaceAR(touches[0].getLocation());

        var subPos = oldPos.sub(newPos); // 2.X版本是 p1.sub(p2);

        self.node.x = self.nodePos.x - subPos.x;
        self.node.y = self.nodePos.y - subPos.y;

        // 控制节点移不出屏幕;
        var minX = -cc.view.getVisibleSize().width / 2 + self.node.width / 2; //最小X坐标;
        var maxX = Math.abs(minX);
        var minY = -cc.view.getVisibleSize().height / 2 + self.node.height / 2; //最小Y坐标;
        var maxY = Math.abs(minY);
        var nPos = self.node.getPosition(); //节点实时坐标;

        if (nPos.x < minX) {
            nPos.x = minX;
        }
        if (nPos.x > maxX) {
            nPos.x = maxX;
        }
        if (nPos.y < minY) {
            nPos.y = minY;
        }
        if (nPos.y > maxY) {
            nPos.y = maxY;
        }
        self.node.setPosition(nPos);
    },
    onTouchEnd() {
        this.nodePos = this.node.getPosition(); //获取触摸结束之后的node坐标;
wangxin's avatar
wangxin committed
86
        if (this._isContact) {
wangxin's avatar
wangxin committed
87
            this.node.position = this._soundOp.position;
wangxin's avatar
wangxin committed
88 89
            if (this._isEnter) {
                console.log("enter right");
wangxin's avatar
wangxin committed
90 91 92
                this.playSound(0);
                var par = cc.instantiate(this.particle_star);
                this.node.addChild(par);
wangxin's avatar
wangxin committed
93 94
            } else {
                console.log("enter false");
wangxin's avatar
wangxin committed
95 96 97 98 99
                this.playSound(1);
                this.node.getComponent(cc.Animation).play("wrong");
                this.scheduleOnce(function () {
                    this.node.setPosition(this._ori_pos);
                }, 2);
wangxin's avatar
wangxin committed
100
            }
wangxin's avatar
wangxin committed
101 102 103 104 105 106 107 108 109 110 111 112
        } else {
            this.node.setPosition(this._ori_pos);
        }
    },

    /**
     * 当碰撞产生的时候调用
     * @param  {Collider} other 产生碰撞的另一个碰撞组件
     * @param  {Collider} self  产生碰撞的自身的碰撞组件
     */
    onCollisionEnter: function (other, self) {
        console.log("on collision");
wangxin's avatar
wangxin committed
113 114
        this._soundOp = other;
        this._isContact = true;
wangxin's avatar
wangxin committed
115 116 117 118 119 120 121 122 123 124 125 126 127
        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
128 129
        this._isContact = false;
        this._soundOp = null;
wangxin's avatar
wangxin committed
130
    },
wangxin's avatar
wangxin committed
131 132 133 134 135

    // 播放声音
    playSound(index) {
        cc.audioEngine.playEffect(this.sounds[index], false);
    },
wangxin's avatar
wangxin committed
136
});