当前位置:网站首页>【cocos creator】点击按钮切换界面
【cocos creator】点击按钮切换界面
2022-07-03 07:44:00 【烧仙草奶茶】
点击按钮切换界面

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;
//按钮
@property([cc.Node])
buttoms: cc.Node[] = [];
//页面
@property([cc.Prefab || cc.Node])
pages: cc.Prefab[] | cc.Node[] = [];
//默认放在主界面中的界面
@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---按钮和页面数量不对应");
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("切换失败!未加载页面" + data.pageName);
return;
}
this.onChangeCheck(data);
this.onSceneChange(data);
}
onMainLoad() {
this._couldTouch = true;
}
/** * 页面切换 * @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 })
}
/** * 加载tab场景 * @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(`加载mainPage错误`, 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;
}
//切换页面前处理
onChangeCheck(data) {
if (data.pageName == this._scenes_now || !this._lode_finish) return;
//页面拉取数据
// 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;
}
//选中的按钮
if (eventName == data.pageName) {
element.scale = 1;
return;
}
//未选中的按钮
element.scale = 0.8;
});
}
onDestroy() {
// EventMgr.ignoreAll(this);
}
}
demo下载:
https://download.csdn.net/download/K86338236/85836249
边栏推荐
- Huawei switch console password reset, device initialization, default password
- Go language foundation ----- 15 ----- reflection
- 研究显示乳腺癌细胞更容易在患者睡觉时进入血液
- Shengsi mindspire is upgraded again, the ultimate innovation of deep scientific computing
- Collector in ES (percentile / base)
- Go language foundation ----- 13 ----- file
- 【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
- Huawei s5700 switch initialization and configuration SSH and telnet remote login methods
- 【MindSpore论文精讲】AAAI长尾问题中训练技巧的总结
- Go language foundation ----- 06 ----- anonymous fields, fields with the same name
猜你喜欢

Harmonyos third training notes

UA camouflage, get and post in requests carry parameters to obtain JSON format content

What to do after the browser enters the URL

An article for you to understand - Manchester code

Redis批量启停脚本

技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型

Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!

技术干货|百行代码写BERT,昇思MindSpore能力大赏
![[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18](/img/9b/db5fe1a37e0de5ba363f9e108310a5.png)
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18

创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
随机推荐
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
[at] abc 258G - Triangle 三元组可达-暴力
Introduction of novel RNA based cancer therapies
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
HarmonyOS第三次培训笔记
Go language foundation ----- 04 ----- closure, array slice, map, package
Go language foundation ----- 13 ----- file
Pat class a 1031 Hello world for u
Hnsw introduction and some reference articles in lucene9
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
Pat class a 1032 sharing
基于RNA的新型癌症疗法介绍
PHP wechat red packet grabbing algorithm
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
PAT甲级 1032 Sharing
【MySQL 12】MySQL 8.0.18 重新初始化
Technical dry goods | some thoughts on the future of AI architecture
Go language foundation ----- 11 ----- regular expression
Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model
Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once