PortraitFixComponent.ts 1.58 KB
import { GameConfig } from "../../GameConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PortraitFixComponent extends cc.Component {

    onLoad() {

    }

    start() {

    }

    update(dt) {

    }

    /**屏幕适配 */
    fixScreenSize(isDatingScene: boolean) {
        let sprComp = this.node.getComponent(cc.Sprite);
        if (!sprComp) {
            cc.error("the PortraitFixComponent node has no cc.Sprite Component !");
            return;
        }
        let spriteFrame = sprComp.spriteFrame;
        if(!spriteFrame){
            cc.warn("the cc.Sprite Component with empty spriteFrame data !");
            return;
        }
        this.node.stopAllActions();
        let spriteFrameRect = spriteFrame.getRect();
        let screenSize = cc.winSize;
        let rW = screenSize.width / spriteFrameRect.width;
        let rH = screenSize.height / spriteFrameRect.height;
        if (GameConfig.debug) {
            // console.log("screenSize.width & spriteFrameRect.width", screenSize.width + ", " + spriteFrameRect.width);
            // console.log("screenSize.height & spriteFrameRect.height", screenSize.height + ", " + spriteFrameRect.height);
            // console.log("rW & rH", rW + ", " + rH);
        }
        if (rW >= 1 && rH >= 1) {
            return;
        }
        let s = 0;
        if (isDatingScene) {
            s = Math.min(rW,rH,1,0.73);
        } else {
            s = Math.min(rW,rH,0.7);   
        }
        if (GameConfig.debug) {
            console.log("this.node.scale", s);
        }
        this.node.scale = s;
    }
}