当前位置:网站首页>[cocos creator] Click the button to switch the interface
[cocos creator] Click the button to switch the interface
2022-07-03 07:53:00 【Braised fairy grass milk tea】
Click button to switch interface
import Util from "./Util";
const {
ccclass, property } = cc._decorator;
@ccclass
export default class mainTab extends cc.Component {
//gamelayer
@property(cc.Node)
gameLayer: cc.Node = null;
// Button
@property([cc.Node])
buttoms: cc.Node[] = [];
// page
@property([cc.Prefab || cc.Node])
pages: cc.Prefab[] | cc.Node[] = [];
// The interface placed in the main interface by default
@property(cc.Node)
public mainPage: cc.Node = null;
_scenes: cc.Node[] = []
_scenes_now: string = "";
_lode_finish: boolean = false;
public _couldTouch: boolean = false;
onLoad(): void {
this.loadScene();
// EventMgr.listen(GameEventType.CHANGE_MAIN_SCENE, this.onChangeMainScene, this);
for (let i = 0; i < this.buttoms.length; i++) {
const element = this.buttoms[i];
if (!this.pages[i]) {
cc.error("mainTab--- Buttons do not correspond to the number of pages ");
return;
}
Util.registerBtnEvent(element, this.changePage, this, this.pages[i].name, {
isScale: false });
}
this.onSceneChange({
pageName: this.mainPage.name });
}
loadScene() {
let sceneName = "mainPage";
this.loadScenes(sceneName, this.mainPage)
}
onChangeMainScene(data) {
let index = this.pages.findIndex((item) => {
return item.name == data.pageName })
if (index == -1) {
cc.error(" Switch failed ! Page not loaded " + data.pageName);
return;
}
this.onChangeCheck(data);
this.onSceneChange(data);
}
onMainLoad() {
this._couldTouch = true;
}
/** * Page switching * @param target * @param name * @returns */
changePage(target: cc.Node, name: string) {
if (!this._couldTouch) {
return;
}
// cc.log(target, name);
// EventMgr.trigger(GameEventType.CHANGE_MAIN_SCENE, { pageName: name });
this.onChangeMainScene({
pageName: name })
}
/** * load tab scene * @param SceneName * @param firstScene * @param cb */
loadScenes(SceneName: string, firstScene: cc.Node, cb?) {
this._lode_finish = false;
this._scenes.push(firstScene);
let js = Util.getScript(firstScene);
if (js) js._init && js._init();
firstScene.active = true;
cc.resources.loadDir(SceneName, (e, res: cc.Prefab[]) => {
if (e) {
console.error(` load mainPage error `, e);
return;
}
for (let i = 0; i < res.length; i++) {
const element = res[i];
if (element.name == firstScene.name) {
continue;
}
let index = this.pages.findIndex((item) => {
return item.name == element.name })
if (index == -1) {
cc.assetManager.releaseAsset(element);
continue;
}
let node: cc.Node = cc.instantiate(element);
if (node) {
this._scenes.push(node);
node.active = false;
node.parent = this.gameLayer;
let js = Util.getScript(node);
if (js) cb && cb(js);
}
}
this._lode_finish = true;
this.onMainLoad()
});
this._scenes_now = firstScene.name;
}
// Before switching pages
onChangeCheck(data) {
if (data.pageName == this._scenes_now || !this._lode_finish) return;
// Page pull data
// if (data.pageName == "") {
// }
this.onChangePage(data);
}
onChangePage(data) {
if (data.pageName == this._scenes_now || !this._lode_finish) return;
this._scenes_now = data.pageName;
for (let i = 0; i < this._scenes.length; i++) {
const element = this._scenes[i];
if (element.name == data.pageName) {
element.active = true;
let js = Util.getScript(element);
if (js) js._init && js._init(data.data);
}
else {
element.active = false;
}
}
}
onSceneChange(data) {
this.buttoms.forEach(element => {
let eventName = "";
if (element.getComponent(cc.Button) && element.getComponent(cc.Button).clickEvents.length) {
eventName = element.getComponent(cc.Button).clickEvents[0].customEventData;
}
// Selected button
if (eventName == data.pageName) {
element.scale = 1;
return;
}
// Unselected button
element.scale = 0.8;
});
}
onDestroy() {
// EventMgr.ignoreAll(this);
}
}
demo download :
https://download.csdn.net/download/K86338236/85836249
边栏推荐
- 输入三次猜一个数字
- C2-关于VCF文件合并的几种方法
- PIP uses image website to solve the problem of slow network speed
- Go language foundation ----- 05 ----- structure
- yarn link 是如何帮助开发者对 NPM 包进行 debug 的?
- PAT甲级 1028 List Sorting
- 创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
- Go language foundation ----- 10 ----- string related operations (operation function, string conversion)
- PAT甲级 1032 Sharing
- Research shows that breast cancer cells are more likely to enter the blood when patients sleep
猜你喜欢
Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function
Docker installs MySQL and successfully uses Navicat connection
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
PAT甲级 1031 Hello World for U
Go language foundation ----- 09 ----- exception handling (error, panic, recover)
Oracle queries grouped by time
Go language foundation ----- 08 ----- interface
Go language foundation ------ 12 ------ JSON
Go language foundation ----- 05 ----- structure
[mindspire paper presentation] summary of training skills in AAAI long tail problem
随机推荐
Go language foundation ----- 13 ----- file
Go language foundation ----- 08 ----- interface
Register keyword
PAT甲级 1027 Colors in Mars
PHP微信抢红包的算法
Oracle queries grouped by time
How to configure GDAL under idea
Worldview satellite remote sensing image data / meter resolution remote sensing image
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
Huawei switch console password reset, device initialization, default password
Usage of (case, when) in PostgreSQL
Pat class a 1032 sharing
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
Precautions for opensips and TLS SIP trunk docking
JS common basic case sorting (continuous update)
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
Redis batch startup and shutdown script
Microsoft Security Response Center
Client server model
LwIP learning socket (application)