CocosCreator適配

推薦閱讀:

今天給大家提供三種CocosCreator常用的三種適配方式,廢話不多說,直接上代碼

/**************************
 * 
 * @author:shirln
 * @time:2019.7.15
 */

cls.Ground = cc.Class({
    extends: cc.Component,

    statics: {
        
        // 所有UserInfo節點列表
        pool: [],

        ///
        // update

    },

    properties: {
        mode: { default: 1 },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        gm.Ground = this;

        // 視口大小
        var csz = cc.view.getCanvasSize();
        // 設計分辨率
        var des = new cc.Vec2(1080, 1920);
        // 進行分辨率適配
        cc.view.setDesignResolutionSize(des.x, des.y, cc.ResolutionPolicy.SHOW_ALL);

        // fitWidth
        var sx = csz.width / des.x;
        // fitHeight
        var sy = csz.height / des.y;
        // show_all
        var min = Math.min(sx, sy);
        // EXACT_FIT
        var max = Math.max(sx, sy);

        /// show_all下縮放背景
        if (this.mode == 1) {
            // 等比例黑邊
            this.node.setScale(max / min, max / min);
        }
        else if (this.mode == 2) {
            // 拉伸填充
            this.node.setScale(sx == min ? 1 : max / min, sy == min ? 1 : max / min);
        }
        else if (this.mode == 3) {
            // 等比例裁剪
            this.node.setScale(max / max, max / max);
        }

    },

    // update (dt) {},

});

使用方法:在需要適配的面板上添加該腳本,併爲其指定適配模式(1,2,3),三選一
這裏需要注意的是,當你使用ScrollView組件時,可能會出現bug:scrollView種超過區域的內容不會被裁剪,例如,紅框範圍爲ScrollView:
在這裏插入圖片描述
解決方法:爲添加view添加widget組件,併爲其指定對其目標爲ScrollView,如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章