PortraitFixComponent.ts
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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;
}
}