1.加载资源
2.将显示对象添加到显示列表
/////////////*****************************动态帧频***********************************///
private imgLoadHandler(e: egret.Event): void {
//输入帧频
this._textInput = new egret.TextField;
this._textInput.size = 50;
this._textInput.type = "input";
this._textInput.width = 300;
this._textInput.height = 60;
this._textInput.border = true;
this._textInput.borderColor = 0x000000;
this._textInput.textAlign = egret.HorizontalAlign.CENTER;
this._textInput.text = "输入帧频";
this._textInput.textColor = 0x0f0f0f;
this._textInput.x = this.stage.stageWidth / 2 - this._textInput.width / 2;
this._textInput.y = 200;
this._textInput.touchEnabled = true;
this.addChild(this._textInput);
this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
//设置帧频
this.stage.frameRate = Number(this._textInput.text);
}, this);
this._textInput.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
this._textInput.text = "";
this._textInput.textColor = 0x000000;
}, this);
//旋转
this.addEventListener(egret.Event.ENTER_FRAME, (e: egret.Event) => {
this.显示对象.rotation += 3;
}, this);
}
/////////////*****************************延迟调用***********************************///
private _txInfo: egret.TextField;
private isComplete: boolean;
private imgLoadHandler(e: egret.Event): void {
//提示信息
this._txInfo = new egret.TextField;
this._txInfo.size = 24;
this._txInfo.textColor = 0x000000;
this._txInfo.lineSpacing = 10;
this._txInfo.multiline = true;
this._txInfo.text = "延迟调用示例
点击舞台显示效果
";
this._txInfo.x = 30;
this._txInfo.y = 30;
this.addChild(this._txInfo);
var self = this;
self.isComplete = true;
var backFun: Function = function () {
self.isComplete = true;
};
//点击舞台调用延迟方法
this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
if (self.isComplete) {
self.isComplete = false;
this.typeEffect(this._txInfo, "每个字符延迟200毫秒调用,实现打字机效果
", 150, backFun);
}
}, this);
}
/**
* 文字打印效果
* obj 文本对象
* content 文字
* interval 打字间隔 毫秒
*/
private typeEffect(obj, content: string = "", interval: number = 200, backFun: Function = null): void {
var strArr: Array<any> = content.split("");
var len: number = strArr.length;
for (var i = 0; i < len; i++) {
egret.setTimeout(function () {
obj.appendText(strArr[Number(this)]);
if ((Number(this) >= len - 1) && (backFun != null)) {
backFun();
}
}, i, interval * i);
}
}
/////////////*****************************定时器***********************************///
private timer: egret.Timer;
private pointer = new egret.Shape();
private onAddTostage(e: egret.Event) {
//生成表盘
var circle = new egret.Shape();
circle.graphics.lineStyle(5, 0x000000, 1, true);
circle.graphics.drawCircle(0, 0, 170);
circle.graphics.endFill();
circle.x = this.stage.stageWidth / 2;
circle.y = this.stage.stageHeight / 2;
this.addChild(circle);
//指针
this.pointer = new egret.Shape();
this.pointer.graphics.beginFill(0x000000);
this.pointer.graphics.drawRect(0, 0, 160, 5);
this.pointer.graphics.endFill();
this.pointer.anchorOffsetY = this.pointer.height / 2;
this.pointer.x = this.stage.stageWidth / 2;
this.pointer.y = this.stage.stageHeight / 2;
this.addChild(this.pointer);
/// 提示信息
this._txInfo = new egret.TextField;
this._txInfo.size = 24;
this._txInfo.textColor = 0x000000;
this._txInfo.lineSpacing = 10;
this._txInfo.multiline = true;
this._txInfo.text = "定时器示例
点击舞台启动或者暂停定时器
";
this._txInfo.x = 30;
this._txInfo.y = 30;
this.addChild(this._txInfo);
var self = this;
this.timer = new egret.Timer(1000, 0);
this.timer.addEventListener(egret.TimerEvent.TIMER, this.timerFunc, this);
this.timer.start();
//点击舞台调用
this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
//定时器正在运行
if (this.timer.running) {
this._txInfo.text += "定时器关闭!
";
this.timer.stop();
} else {
this._txInfo.text += "定时器开启!
";
this.timer.start();
}
}, this);
}
//指针按照定时每次旋转6度,60秒旋转一周
private timerFunc(e: egret.Event) {
this.pointer.rotation += 6;
if (this.pointer.rotation > 360) {
this.pointer.rotation -= 360;
}
}
http://developer.egret.com/cn/example/egret2d/index.html#080-run-frame-rate