KUOKUO的物理小游戏(二)

Creator星球游戏开发社区

共 2398字,需浏览 5分钟

 ·

2020-08-13 12:29

摘要

CocosCreator 物理小游戏实战《KUOKUO的物理小游戏(一)的视频教程也开更啦!来哔哩哔哩关注 KUOKUO 一起学习吧!视频连接:https://www.bilibili.com/video/BV1ck4y167mR

使用版本

CocosCreator 版本 2.3.4

资源划分

素材导入后进行资源分类,我这里将其分为 game 文件夹与 ui 文件夹,对应游戏场景使用资源和显示 UI 资源。

静态单例管理

UIManager 与 GameManger 分别绑定至管理节点,两者通讯靠 StaticInstance 脚本。

  1. import GameManager from "./GameManager"

  2. import UIManager from "./UIManager"


  3. export class StaticInstance {


  4. static gameManager: GameManager | undefined = undefined

  5. static uiMannager: UIManager | undefined = undefined


  6. static setGameManager(context: GameManager) {

  7. StaticInstance.gameManager = context

  8. }


  9. static setUIManager(context: UIManager) {

  10. StaticInstance.uiMannager = context

  11. }


  12. }

两个 Manager 脚本的 onLoad 中传入实例,在后面就可以相互调用了。

  1. import { StaticInstance } from "./StaticInstance"


  2. const {ccclass, property} = cc._decorator


  3. @ccclass

  4. export default class GameManager extends cc.Component {


  5. onLoad() {

  6. StaticInstance.setGameManager(this)

  7. }


  8. }

在 start 及其以后即可访问实例。

  1. import { StaticInstance } from "./StaticInstance"


  2. const {ccclass, property} = cc._decorator


  3. @ccclass

  4. export default class UIManager extends cc.Component {


  5. onLoad() {

  6. StaticInstance.setUIManager(this)

  7. }


  8. start () {

  9. console.log(StaticInstance.gameManager)

  10. }


  11. }

这样就可以实现 UIManager 与 GameManager 的相互调用。

继承UIBase

我们有 6 个 UI 要进行管理,每个 UI 都应该有显示与隐藏的方法,故抽出其共有属性与方法,实现一个 UIBase,让 6 个 UI 脚本去继承 UIBase 即可。

  1. const {ccclass, property} = cc._decorator


  2. @ccclass

  3. export default class UIBase extends cc.Component {


  4. @property({

  5. displayName: '初始显隐状态'

  6. })

  7. isShowInit: boolean = false


  8. onLoad() {

  9. this.isShowInit ? this.show() : this.hide()

  10. }


  11. show() {

  12. this.node.active = true

  13. }


  14. hide() {

  15. this.node.active = false

  16. }


  17. }

StartMenu制作

拖入对应的 ui 图片完成布局,然后做成预制体。

声明两个按钮属性,写下几个方法,注意是继承 UIBase 的。

  1. import UIBase from "./UIBase"

  2. import UIManager from "../UIManager"


  3. const {ccclass, property} = cc._decorator


  4. @ccclass

  5. export default class StartMenu extends UIBase {


  6. @property(cc.Node) startButton: cc.Node = undefined

  7. @property(cc.Node) levelSelectButton: cc.Node = undefined


  8. onLoad() {

  9. super.onLoad()

  10. }


  11. show() {

  12. super.show()

  13. }


  14. init(uiManager: UIManager) {


  15. }


  16. }

如下 UML 图,在 StartMenu 中就拥有了这些属性方法。

结语

视频已经开始更了哦,视频链接为:https://www.bilibili.com/video/BV1ck4y167mR

2020!我们一起进步!

浏览 27
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报