cocos creator全域性變數與常住節點

2021-09-11 23:32:40 字數 1413 閱讀 3099

1, 使用 通過模組訪問

建立乙個helloworld專案。

建立乙個指令碼,來儲存變數,common.js

module.exports = ,

text: 'hello, world!'

},// use this for initialization

onload: function () ,

//跳轉場景

setscene : function(),

// called every frame

update: function (dt) ,

});新建乙個場景,demo,並新建乙個指令碼檔案。demo.js。並繫結指令碼給場景

同理,在demoscene指令碼中引用common指令碼。並且列印在上乙個場景所傳入的值

var com = require('common');

cc.class(,

// ...

},// use this for initialization

onload: function () ,

// called every frame, uncomment this function to activate update callback

// update: function (dt) ,

});這是全域性變數的乙個使用方法

還有一種是設定常駐節點

在helloworld場景的層級管理中,新建節點,我是建立乙個根節點

並且給該節點繫結乙個指令碼,

這個指令碼的內容:

cc.class(,

// use this for initialization

onload: function () ,

//自定義的兩個函式。將值儲存在this變數裡

setdata : function(json),

getdata : function(),

});helloworld.js

var com = require('common');

cc.class(,

text: 'hello, world!'

},// use this for initialization

onload: function () ,

//跳轉場景

setscene : function(),

// called every frame

update: function (dt) ,

});第二個場景,demoscene.js

var com = require('common');

cc.class(,

// use this for initialization

onload: function () ,

});執行結果,場景條轉後,值還是存在的。

CocosCreator全域性訊息事件

我的git有 如下 notificationcenter.ts import from notificationname cocoscreator s global event message center.export class notificationcenter return this in...

nuxt專案中使用less全域性樣式 全域性變數的配置

使用less先安裝less less loader,nuxt會自動識別匯入的字尾名,在nuxt.config.js中配置 css 配置全域性 css element ui lib theme chalk index.css assets styles global.less 為頁面注入變數可以使用 ...

static全域性 區域性變數,全域性變數及區域性變數

1 static變數和全域性變數都是分配在 靜態儲存區 2 static全域性變數 static區域性變數 全域性變數和區域性變數區別 他們都是分配在 靜態儲存區內,都是在程式開始的時候初始化的,他們的區別是作用域不同,static全域性變數作用域是在申明所在的檔案中,而全域性變數是全域性的,其他檔...