cocos-enhance-kit/demo/assets/text-render/char-mode/char-mode.ts

75 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2022-06-17 10:42:43 +00:00
import { chars } from "../chars";
const { ccclass, property } = cc._decorator;
@ccclass
export default class CharMode extends cc.Component {
@property(cc.Label)
bitmapFont1: cc.Label = null;
@property(cc.Label)
bitmapFont2: cc.Label = null;
@property(cc.Label)
bitmapFont3: cc.Label = null;
2022-06-20 09:43:35 +00:00
@property(cc.Label)
bitmapFont4: cc.Label = null;
2022-06-17 10:42:43 +00:00
@property(cc.Label)
charFont1: cc.Label = null;
@property(cc.Label)
charFont2: cc.Label = null;
@property(cc.Label)
charFont3: cc.Label = null;
2022-06-20 09:43:35 +00:00
@property(cc.Label)
charFont4: cc.Label = null;
2022-06-17 10:42:43 +00:00
protected onLoad(): void {
this.bitmapFont1.string = this.getRandomText(15);
this.bitmapFont2.string = this.getRandomText(15);
this.bitmapFont3.string = this.getRandomText(15);
2022-06-20 09:43:35 +00:00
this.bitmapFont4.string = this.getRandomTextEnglish(30);
2022-06-17 10:42:43 +00:00
this.charFont1.string = this.getRandomText(15);
this.charFont2.string = this.getRandomText(15);
this.charFont3.string = this.getRandomText(15);
2022-06-20 09:43:35 +00:00
this.charFont4.string = this.getRandomTextEnglish(30);
2022-06-17 10:42:43 +00:00
this.schedule(() => {
this.bitmapFont1.string = this.getRandomText(15);
this.bitmapFont2.string = this.getRandomText(15);
this.bitmapFont3.string = this.getRandomText(15);
2022-06-20 09:43:35 +00:00
this.bitmapFont4.string = this.getRandomTextEnglish(30);
2022-06-17 10:42:43 +00:00
this.charFont1.string = this.getRandomText(15);
this.charFont2.string = this.getRandomText(15);
this.charFont3.string = this.getRandomText(15);
2022-06-20 09:43:35 +00:00
this.charFont4.string = this.getRandomTextEnglish(30);
2022-06-17 10:42:43 +00:00
}, 0.1);
}
getRandomText(length: number) {
let str = '';
while (str.length < length) {
str += chars[Math.floor(Math.random() * chars.length)];
}
return str;
}
2022-06-20 09:43:35 +00:00
enChars = "abcdefghijklmnopqrstuvwxyz.,/ 1234567890!@#$%^&*()-=_+`~,./;'<>?;[]{}|";
getRandomTextEnglish(length: number) {
let str = '';
while (str.length < length) {
str += this.enChars[Math.floor(Math.random() * this.enChars.length)];
}
return str;
}
2022-06-17 10:42:43 +00:00
}