其实LayaBox加载Json还是蛮简单的,但是作为新手来说,其实也不容易,下面放出解决方法,在类比一下就知道有多容易了。
新建laya.json文件放入到bin目录下
// laya.json{
"name": "deng",
"age": 17,
"sex": "male",
"phone:": "12345678"
}
// LayaSample.ts// 程序入口
class GameMain{
constructor()
{
Laya.init(600,400); Laya.loader.load("laya.json", Laya.Handler.create(this, this.onLoaded), null, Laya.Loader.JSON); }
private onLoaded() {
var json: JSON = Laya.loader.getRes("laya.json"); console.log(json["name"]); console.log(json["age"]); console.log(json["sex"]); console.log(json["phone"]); }
}
new GameMain();
最后输出结果:
deng
17
male
12345678
网友评论