Egret开放平台

egret知(填)识(坑)点集

2018-04-25  本文已影响58人  陨石坠灭

1. egret.Label 富文本用法

...
public lbTest:eui.Label;
...

lbTest.textFlow=[
  {text:"hello",style:{size:12,textColor:oxffffff}},
   {text:"hello",style:{size:16,textColor:oxffff00}}
]

富文本还可以添加链接,同时可以给链接添加下划线哦 ^0^

var ADDRESS = "egret知(填)识(坑)点集";
lbTest.textFlow=[
...
{
  text: ADDRESS,
  style: { textColor: 0x76b7e8, size: 21, href:"https://www.jianshu.com/p/a4d600ce46af", underline: true },
}
...
]

给链接添加事件:

var ADDRESS = "egret知(填)识(坑)点集";
lbTest.textFlow=[
{
  text: ADDRESS,
  style: { textColor: 0x76b7e8, size: 21, href:"event:gotoPage", underline: true },
}
...
]

lbTest.addEventListener( egret.TextEvent.LINK, function( evt:egret.TextEvent ){
      console.log( evt.text );
      if(isMobile){
          evt.preventDefault();
      }
      window.open("https://www.jianshu.com/p/a4d600ce46af","_blank");
}, this );

但是这样写可能会带来坑,在监听egret.TextEvent.LINK事件的函数内跳转链接,会被浏览器阻止!!!

因此,兼容移动端时,应该这么些:

var ADDRESS = "egret知(填)识(坑)点集";
lbTest.textFlow=[
{
  text: ADDRESS,
  style: { textColor: 0x76b7e8, size: 21, href: isMobile ? "event:gotoPage" : "https://www.jianshu.com/p/a4d600ce46af", underline: true },
}
...
]

lbTest.addEventListener( egret.TextEvent.LINK, function( evt:egret.TextEvent ){
      console.log( evt.text );
      if(isMobile){
          evt.preventDefault();
      }
}, this );

具体情况,请看 论egret的坑

2. 骨骼动画

加载:

 let dragonebonesData = RES.getRes(<*.json>);
 let textureData = RES.getRes(<*_ske.json>); 
 let texture = RES.getRes(<*.png>);
 let factory:dragonBones.EgretFactory = new dragonBones.EgretFactory();
 factory.addDragonBonesData(dragonBones.DataParser.parseDragonBonesData(dragonebonesData));
 factory.addTextureAtlas(new dragonBones.EgretTextureAtlas(texture , textureData));

 var armature:dragonBones.FastArmature = factory.buildFastArmature(armatureName);
 armature.enableAnimationCache(30); 
...

替换控件,方法为dragonBones.Slot.setDisplay(dispaly:any),但是并不是每一次都能成功设置上,display也并不是一直有值,以下提供一种方式参考,并不一定对哈:

private testGroup:eui.Group;//参考点
...

armature.addEventListener(dragonBones.AnimationEvent.START, function(){
  egret.setTimeout(function(){
    let group = this.testGroup;
    let slot: dragonBones.Slot = armature.getSlot("<骨骼名称>");
    let cell = new <控件类>();
    cell.width =group.width;
    cell.height = group.height;
    cell.anchorOffsetX = group.width / 2;
    cell.anchorOffsetY = group.height / 2;
    slot.setDisplay(cell);    
 },this,200);
},this);  

3. 事件

按钮点击事件

private btnTest:eui.Button;
...
btnTest.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouch,this);
...
private onTouch(event){
  switch(event.target){
     case this.btnTest:{
        //TODO
      }
  }
}

控件加入和移除事件:

this.addEventListener(egret.Event.ADDED_TO_STAGE...
this.addEventListener(egret.Event.REMOVED_FROM_STAGE...

定时执行

...
let timer:egret.Timer = new egret.Timer(RETRY_INTERVALS[this._retries], 1);
timer.addEventListener(egret.TimerEvent.TIMER,this.text, this);
timer.start();
...
private test(event){
//TODO
}

4. 控件状态替换

class TestLayer eui.Component implements  eui.UIComponent{
...
this.skin.currentState = this.skin.states[0].name;
...
}

5.获取时间的方式

...
let date = Date.now()
...

6.http请求

http://edn.egret.com/cn/article/index/id/589


10.13更新

7.移动端接口适配

 interface MobileInterface {
        ...
        openLink(url:string):void
        ...
    }

    export const API: MobileInterface = window['iface'];

8.获取url参数

var id = egret.getOption('id');

9.Object.keysObject.values的使用

var obj = {};
var keys = (<any>Object).keys(obj);
var values = (<any>Object).values(obj);

10.StringNumberMath等方法的拓展

interface String {
...
  format(...args:any[]): string;
}

String.prototype.format = function () {
    var a = this;
    for (k in arguments) {
        a = a.replace("{" + k + "}", arguments[k]);
    }
    return a;
}
上一篇下一篇

猜你喜欢

热点阅读