当前位置:网站首页>【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
边栏推荐
- C2 several methods of merging VCF files
- Enter three times and guess a number
- How to clear the console password for s7700 device
- Huawei s5700 switch initialization and configuration Telnet, SSH user methods
- 密西根大学张阳教授受聘中国上海交通大学客座教授(图)
- 项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
- Screenshot tool snipaste
- 技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
- Go language foundation ----- 11 ----- regular expression
- 技术干货|昇思MindSpore Lite1.5 特性发布,带来全新端侧AI体验
猜你喜欢
Pat class a 1032 sharing
Go language foundation ------ 14 ------ gotest
Go language foundation ----- 09 ----- exception handling (error, panic, recover)
Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function
HDMI2.1与HDMI2.0的区别以及转换PD信号。
【MindSpore论文精讲】AAAI长尾问题中训练技巧的总结
PAT甲级 1029 Median
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
一篇文章让你读懂-曼彻斯特编码
Technical dry goods Shengsi mindspire innovation model EPP mvsnet high-precision and efficient 3D reconstruction
随机推荐
IndexSort
【MySQL 13】安装MySQL后第一次修改密码,可以可跳过MySQL密码验证进行登录
Structure of golang
项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once
Hnsw introduction and some reference articles in lucene9
技术干货|关于AI Architecture未来的一些思考
tp3.2和tp5.0的区别
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
What did the DFS phase do
Redis view client connection
Redis查看客户端连接
WorldView卫星遥感影像数据/米级分辨率遥感影像
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
Redis配置文件
Technical dry goods | some thoughts on the future of AI architecture
Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model
Go language foundation ----- 15 ----- reflection
【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机
项目经验分享:基于昇思MindSpore实现手写汉字识别