ItemQuestion.js 6.11 KB
Newer Older
asdf's avatar
.  
asdf committed
1 2 3 4
cc.Class({
    extends: cc.Component,

    properties: {
asdf's avatar
asdf committed
5 6 7 8 9 10
        nodBg1: cc.Node,
        nodBg2: cc.Node,
        dragonBone: dragonBones.ArmatureDisplay,
        labQuestion1: cc.Label,
        labQuestion2: cc.Label,
        labQuestion3: cc.Label,
asdf's avatar
.  
asdf committed
11 12 13
    },
    // onLoad () {},

asdf's avatar
asdf committed
14
    start() {
asdf's avatar
.  
asdf committed
15 16

    },
asdf's avatar
asdf committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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

    onEnable() {
        this.node.on('touchstart', this.onTouchStart, this);
    },
    onDisable() {
        this.node.off('touchstart', this.onTouchStart, this);
    },

    setInfo(data, index, scene) {
        this.scene = scene;
        this.data = data;
        this.index = index;
        this.updateLabels();
        this.loadDragonBone();
    },

    loadDragonBone() {
        this.dragonBone.dragonAtlasAsset = null;
        this.dragonBone.dragonAsset = null;
        this.dragonBone.armatureName = '';
        var imageUrl = this.data.optionDragonBone.texPngData.url;
        var skeUrl = this.data.optionDragonBone.skeJsonData.url;
        var atlasUrl = this.data.optionDragonBone.texJsonData.url;
        // imageUrl = 'http://127.0.0.1/NewProject_2_tex.png';
        // skeUrl = 'http://127.0.0.1/NewProject_2_ske.json';
        // atlasUrl = 'http://127.0.0.1/NewProject_2_tex.json';

        // imageUrl = 'http://127.0.0.1/walkingfish_tex.png';
        // skeUrl = 'http://127.0.0.1/walkingfish_ske.json';
        // atlasUrl = 'http://127.0.0.1/walkingfish_tex.json';
        cc.assetManager.loadAny([{ url: atlasUrl, ext: '.txt' }, { url: skeUrl, ext: '.txt' }], (error, assets) => {
            if (error) {
                console.log(error)
            }
            else {
                cc.assetManager.loadRemote(imageUrl, (error, texture) => {
                    if (error) {
                        console.log(error)
                    }
                    else {
                        var atlas = new dragonBones.DragonBonesAtlasAsset();
                        atlas._uuid = atlasUrl;
                        atlas.atlasJson = assets[0];
                        atlas.texture = texture;

                        var asset = new dragonBones.DragonBonesAsset();
                        asset._uuid = skeUrl;
                        asset.dragonBonesJson = assets[1];

                        this.dragonBone.dragonAtlasAsset = atlas;
                        this.dragonBone.dragonAsset = asset;
                        let data = asset._dragonBonesJsonData.armature[0];
                        if (!data) {
                            return;
                        }
                        this.dragonBone.armatureName = data.name;
                        this.animationName = data.animation[0].name;

asdf's avatar
asdf committed
75 76
                        // this.dragonBone.premultipliedAlpha = true;
                        // atlas.texture.setPremultiplyAlpha(true);
asdf's avatar
asdf committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
                    }
                });
            }
        });
    },

    updateLabels() {
        this.labQuestion1.string = this.data.text;
        this.labQuestion2.string = this.data.text;
        this.labQuestion3.string = this.data.text;
        this.labQuestion1._forceUpdateRenderData();
        this.labQuestion2._forceUpdateRenderData();
        this.labQuestion3._forceUpdateRenderData();

        if (this.labQuestion1.node.width > 145) {
            this.labQuestion1.node.scale = 145 / this.labQuestion1.node.width;
            this.labQuestion2.node.scale = 145 / this.labQuestion1.node.width;
            this.labQuestion3.node.scale = 145 / this.labQuestion1.node.width;
        }
asdf's avatar
.  
asdf committed
96
    },
asdf's avatar
.  
asdf committed
97

asdf's avatar
asdf committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    setChooseRight(dragonBone, animationName) {
        this.isRight = true;
        this.animationName2 = animationName;
        this.nodBg1.active = false;
        this.nodBg2.active = true;
        this.labQuestion1.node.parent.active = false;
        this.labQuestion2.node.parent.active = false;
        this.labQuestion3.node.active = true;
        this.nodDragonBone2 = cc.instantiate(dragonBone.node);
        this.nodDragonBone2.parent = this.node;
        this.nodDragonBone2.x = 40;
        this.nodDragonBone2.y = this.dragonBone.node.y;
        this.dragonBone.node.x = -40;
        this.playAudioOption(true);

        this.dragonBone.playAnimation(this.animationName, 0);
        this.nodDragonBone2.getComponent(dragonBones.ArmatureDisplay).playAnimation(animationName, 0);
    },
    stopRightAnimation() {
        this.dragonBone.playAnimation(this.animationName, 1);
        this.nodDragonBone2.getComponent(dragonBones.ArmatureDisplay).playAnimation(this.animationName2, 1);
    },

    setGray() {
        this.isGray = true;
        this.labQuestion1.node.parent.active = false;
        this.labQuestion2.node.parent.active = false;
        this.nodBg1.color = new cc.Color().fromHEX('#7E7E7E');
        this.dragonBone.node.color = new cc.Color().fromHEX('#7E7E7E');
    },
    setNormal() {
        this.isGray = false;
        this.nodBg1.color = new cc.Color().fromHEX('#ffffff');
        this.dragonBone.node.color = new cc.Color().fromHEX('#ffffff');
    },

    onTouchStart() {
asdf's avatar
.  
asdf committed
135 136 137
        if (GameData.isMoving) {
            return;
        }
asdf's avatar
asdf committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        if (this.isRight) {
            return;
        }
        if (this.isGray) {
            return;
        }
        if (GameData.isAudio) {
            return;
        }
        this.showQipao();
        this.playAudioOption();
    },

    showQipao() {
        if (this.index == 3) {
            this.labQuestion1.node.parent.active = false;
            this.labQuestion2.node.parent.active = true;
        } else {
            this.labQuestion1.node.parent.active = true;
            this.labQuestion2.node.parent.active = false;
        }
    },

    playAudioOption(showNext) {
        GameData.isAudio = true;
        let url = this.data.optionAudio;
        // url = 'http://127.0.0.1/iamflying.mp3';
        cc.assetManager.loadRemote(url, null, (err, clip) => {
            if (err) {
                console.log(err)
                return;
            }
            let id = cc.audioEngine.play(clip, false, 1);
            cc.audioEngine.setFinishCallback(id, () => {
                GameData.isAudio = false;
                if (showNext) {
                    this.scene.showNext(this.index);
                }
            })
        });
    },
asdf's avatar
.  
asdf committed
179 180
    // update (dt) {},
});