forked from caochao/cocos_creator_proj_base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
140 lines (121 loc) · 3.2 KB
/
utils.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {loader_mgr} from "../common/loader/loader_mgr"
import * as consts from "../consts"
let handler_pool:handler[] = [];
let handler_pool_size = 10;
//用于绑定回调函数this指针
export class handler
{
private cb:Function;
private host:any;
private args:any[];
private times:number;
private is_persist:boolean;
constructor(){}
init(cb:Function, host:any = null, ...args:any[]):void
{
this.cb = cb;
this.host = host;
this.args = args;
this.times = 0;
this.is_persist = false;
}
exec(...extras:any[]):void
{
//持久的handler, exec可能会执行多次,每次执行前要清理一下旧参数
if(this.is_persist && this.times > 0)
{
this.args = [];
cc.info("handler exec", this.times, "times, clear prev args");
}
this.args = this.args.concat(extras);
this.cb.apply(this.host, this.args);
this.times++;
//临时的用完回收
if(!this.is_persist)
{
this.release();
}
}
retain()
{
this.is_persist = true;
}
release()
{
// this.cb = null;
// this.host = null;
// this.args = null;
// this.times = null;
// this.is_persist = false;
// if(handler_pool.length < handler_pool_size)
// {
// handler_pool.push(this);
// }
}
}
/**注意区分一次性和持久的handler */
export function gen_handler(cb:Function, host:any = null, ...args:any[]):handler
{
let single_handler:handler = handler_pool.length < 0 ? handler_pool.pop(): new handler()
//这里要展开args, 否则会将args当数组传给wrapper, 导致其args参数变成2维数组[[]]
single_handler.init(cb, host, ...args);
return single_handler;
}
//本地存储
export class localStorage
{
static set(key:string, value:any):void
{
cc.sys.localStorage.setItem(key, value);
}
static get(key:string):string
{
return cc.sys.localStorage.getItem(key);
}
static remove(key:string):void
{
cc.sys.localStorage.removeItem(key);
}
static set_json(key:string, obj:any):void
{
this.set(key, JSON.stringify(obj));
}
static get_json(key:string):any
{
return JSON.parse(this.get(key));
}
static get_float(key:string):number
{
let str_val = this.get(key);
if(str_val)
{
return parseFloat(str_val);
}
return null;
}
static get_int(key:string):number
{
let str_val = this.get(key);
if(str_val)
{
return parseInt(str_val);
}
return null;
}
}
export function load_head(sprite, head_name)
{
if(!head_name || head_name == "")
{
head_name = consts.MISC.DEFAULT_HEAD;
}
loader_mgr.get_inst().loadAsset("heads/dz_head", gen_handler((atlas:cc.SpriteAtlas) => {
sprite.spriteFrame = atlas.getSpriteFrame(head_name);
}), cc.SpriteAtlas);
}
export function load_img(sprite, img_path)
{
loader_mgr.get_inst().loadAsset(img_path, gen_handler((res) => {
sprite.spriteFrame = res;
}), cc.SpriteFrame);
}