egret(白鹭引擎)编程笔记
ChrisXie Lv4

Egret自用开发环境

Egret Wing 添加自动格式化

文件》首选项》工作区选项,进入settings.json文件替换下方代码即可

1
2
3
4
5
6
7
8
9
10
{
"editor.formatOnType": true,
"editor.formatOnSave": true,
"search.exclude": {
"**/bin-debug": true,
"**/bin-release": true

}

}


Egret eui.Scroller 滚动条不显示设置

不显示垂直方向的滚动条

1
2
3
//需要在scroller添加到舞台上面之后再访问verticalScrollBar
scroller.verticalScrollBar.autoVisibility = false;
scroller.verticalScrollBar.visible = false;


egret白鹭引擎微信小游戏设置沉浸模式

在微信小游戏项目中的game.json文件中添加showStatusBar并且设置为true。


1
"showStatusBar": true,

egret白鹭引擎微信小游戏获取状态栏的高度

获取的方式都是可以通过获取系统系统的方式去获取statusBarHeight
wx.getSystemInfoSync
wx.getSystemInfoAsync
wx.getSystemInfo

在egret项目中Platform.ts添加:

1
2
3
4
5
6
7
8
9
10
11
12
//正式版中只有方法声明
declare interface Platform {
//获取微信端系统信息
getSystemInfoSync(): Promise<any>;

}
//测试版
class DebugPlatform implements Platform {
async getSystemInfoSync() {
return { wxSystemInfo: "wxSystemInfo" }
}
}

在Main.ts中获取信息

1
2
let systemInfo: any = platform.getSystemInfoSync();
console.log(systemInfo);

微信小游戏项目中platform.js 中添加

1
2
3
4
//获取微信端系统信息
getSystemInfoSync(){
return wx.getSystemInfoSync();
}

egret白鹭引擎微信小游戏获取获取菜单按钮(右上角胶囊按钮)的布局位置信息

获取的方式都是可以通过获取getMenuButtonBoundingClientRect
wx.getMenuButtonBoundingClientRect

在egret项目中Platform.ts添加:

1
2
3
4
5
6
7
8
9
10
11
12
//正式版中只有方法声明
declare interface Platform {
//获取菜单按钮(右上角胶囊按钮)的布局位置信息
getMenuButtonBoundingRect(): Promise<any>;

}
//测试版
class DebugPlatform implements Platform {
async getMenuButtonBoundingRect() {
return { wxmenuBtutton: "wxmenuBtutton" }
}
}

在Main.ts中获取信息

1
2
let menuButtonInfo: any = platform.getMenuButtonBoundingRect();
console.log(menuButtonInfo);

微信小游戏项目中platform.js 中添加

1
2
3
4
//获取微信端系统信息
getMenuButtonBoundingRect() {
return wx.getMenuButtonBoundingClientRect();
}

egret根据文本宽度获取超过文本宽度的文本是在第几个

1
2
3
4
5
6
7
8
9
10
11
private getCharIndexAtPoint(_textfield: egret.TextField, num: number = 0): any {
var _matchWidth = egret.sys.measureText(_textfield.text, _textfield.fontFamily, _textfield.size, _textfield.bold, _textfield.italic);//size 单个文本字体宽度
if (_matchWidth > _textfield.width) {
var lines = _textfield.$getLinesArr();//获取的是富文本下的形式
if (lines.length > 1) {
var elements = lines[0].elements[0];
var charLength = lines[0].elements[0].text.length;
}
}
return charLength - num;
}

egret白鹭引擎通过代码控制console函数是否输出日志:

1
2
3
4
5
6
7
8
9
10
11
12
//判断配置文件是否开启日志调试 是否输出日志 True 输出 False 不输出
var logDebug = false;
if (DEBUG) {//egret 是否本地调试还是发布版本
logDebug = true;
}
console.log = (function (oriLogFunc) {
return function () {
if (logDebug) {
oriLogFunc.apply(this, arguments);
}
};
})(console.log);

egret通过发布的版本号转换成年月日时间显示

1
2
3
4
5
6
7
8
9
10
11
12
13
egret通过发布的版本号转换成年月日时间显示
var timeStrAry = ["年", "月", "日", "时", "分", "秒"];
var timeStr = "";
var Str = "220525094609";
for (var i = 0; i < timeStrAry.length; i++) {
timeStr += Str.substr((i * 2), 2) + timeStrAry[i];
}
let timeText = new egret.TextField();
timeText.text = timeStr;
timeText.x = 200;
timeText.y = 300;
timeText.textColor = 0xFFCC33;
this.addChild(timeText);

生成一段到100000的随机数

1
2
3
4
5
6
生成一段到100000的随机数
var matNum = Math.ceil(Math.random() * 100000);
console.log(matNum);
console.log(new Date().getTime());
生成一个当前时间段的时间戳
new Date().getTime()
 评论
相关文章
标签云 更多