当前位置:网站首页>【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
边栏推荐
- The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
- PHP常用排序算法
- [at] abc 258G - Triangle 三元組可達-暴力
- Huawei s5700 switch initialization and configuration Telnet, SSH user methods
- IndexSort
- Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition
- PAT甲级 1027 Colors in Mars
- Pat class a 1030 travel plan
- Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
- Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
猜你喜欢
Screenshot tool snipaste
技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
技术干货|关于AI Architecture未来的一些思考
[mindspire paper presentation] summary of training skills in AAAI long tail problem
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
Go language foundation ----- 05 ----- structure
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
技术干货|昇思MindSpore Lite1.5 特性发布,带来全新端侧AI体验
Pat class a 1032 sharing
【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机
随机推荐
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
Pat class a 1031 Hello world for u
【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
How does yarn link help developers debug NPM packages?
Analysis of the problems of the 7th Blue Bridge Cup single chip microcomputer provincial competition
Professor Zhang Yang of the University of Michigan is employed as a visiting professor of Shanghai Jiaotong University, China (picture)
Segment read
【踩坑系列】mysql 修改root密码失败
PAT甲级 1032 Sharing
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
PAT甲级 1030 Travel Plan
Go language foundation ----- 10 ----- string related operations (operation function, string conversion)
Static keyword
C2 several methods of merging VCF files
Robots protocol
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
Harmonyos third training notes
Huawei s5700 switch initialization and configuration Telnet, SSH user methods
Redis profile