Quick小白书系列三MyApp详解

本来是计划从这篇开始做个用一个小游戏教程来讲解Quick的使用,但是发现很多童鞋对Quick的框架还真是不了解。

其实这种不了解源自于没有认真的阅读Quick的framework的代码,但无论是懒得读还是读不懂,结果都是会问各种基础的小白问题,于是我决定这个系列还继续讲解一些小白类的问题,而一些略微深入的使用以及我在做项目中得一些值得分享的东西我会单独开贴来写!


好了,进入今天正题,在上一篇内容中,我已经说明了Quick得项目启动过程中,看过的应该知道Quick启动中一个关键的类就是MyApp,所以本篇就来详细讲解下这个东西。


首先,打开自己新建项目的MyApp.lua文件代码我就不贴了,下面开始逐步讲解!

1.通过一些文件进行framework的初始化

在MyApp.lua的最开始是3行require语句

1
2
3
require( "config" )
"cocos.init" )
"framework.init" )

在Lua中,require表示引入一个文件,那么上面的三个文件分别在哪里呢?

他们分别在:

  • root/src/config.lua

  • root/src/cocos/init.lua

  • root/src/framework/init.lua

其中root表示你项目的根目录。

通过require这三个文件后,将会对Quick的框架做一些初始化的事情。简单说下是如何初始化的,比如,require("config"),这句会加载root/src/config.lua文件,require加载文件的过程中会检查该文件的lua语法,以及完成文件内的一些变量的初始化,你打开config.lua文件,你回看到都是一些变量的初始化,这些都是全局变量,因为没有local,看过Lua语法的都晓得。require了之后,你就可以在任何地方引用这些变量,比如DEBUG变量。关于Lua的require更详细明确的讲解,请自行百度。这里,你就可以先理解为引入三个文件并且进行初始化。

2.MyApp的定义

回到MyApp文件,看这一行

1
localMyApp= class ( "MyApp" ,cc.mvc.AppBase)

这一行的意思是:定义一个MyApp类,它继承自cc.mvc.AppBase。

那么问题来了,cc.mvc.AppBase在哪里?在root/framework/cc/mvc/AppBase.lua

理解继承概念的童鞋一看则明,不了解何为继承的,可以简单的这样理解:MyApp继承了AppBase,那么MyApp就可以使用AppBase的所有变量和函数,就跟MyApp自己的一样,如何使用,下面会具体介绍

3.MyApp的构造函数

MyApp:ctor这个函数理解为构造函数

MyApp.super.ctor(self),这一行的意思是调用基类的构造函数,也就是AppBase的构造函数

那么我们来看看AppBase的ctor函数都干了啥,打开AppBase,看ctor函数

cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()

这一行是给AppBase函数添加事件组件,至于事件组件是啥,后面会有专门对这个组件讲解,这里你知道是添加了事件组件就好

2
self.name=appName
self.packageRoot=packageRootor "app"

这里就是简单地两个赋值,self就是AppBase自己

3
4
5
6
7
localeventdispatcher=cc.Director:getInstance():getEventdispatcher()
localcustomListenerBg=cc.EventListenerCustom:create(AppBase.APP_ENTER_BACKGROUND_EVENT,
handler(self,self.onEnterBackground))
eventdispatcher:addEventListenerWithFixedPriority(customListenerBg,1)
localcustomListenerFg=cc.EventListenerCustom:create(AppBase.APP_ENTER_FOREGROUND_EVENT,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,self.onEnterForeground))
eventdispatcher:addEventListenerWithFixedPriority(customListenerFg,1)

这几行内容是通过eventdispatcher注册两个监听事件,分别是AppBase.APP_ENTER_BACKGROUND_EVENT和AppBase.APP_ENTER_FOREGROUND_EVENT,这两个事件是你的游戏在切换到后台和从后台切换回来的时候触发后,会调用AppBase的两个方法onEnterBackground,onEnterForeground。这两个方法的定义在AppBase的下面,因为涉及到事件的东西,暂时用不到,所以不做详解。

self.snapshots_={}
--setglobalapp
app=self

这两行是初始化两个变量,注意app这个变量的初始化,app被定义为一个全局变量,并且赋值为self,也就是以后你使用app这个全局变量的时候它就是AppBase,或者继承自AppBase的MyApp。

4.run函数

cc.FileUtils:getInstance():addSearchPath("res/" 这一行是向搜索路径中添加res目录,何为搜索路径,就是你将来引用资源的时候能够查找的目录。

self:enterScene("MainScene" 这一句有童鞋会不理解,enterScene方法哪里来得,MyApp中没有定义啊,是得MyApp中没有定义,因为是在AppBase中定义的,而上面说了,通过继承可以直接访问基类的方法,所以MyApp可以直接使用enterScene方法

在AppBase中找到enterScene方法的定义。

enterScene(sceneName,args,transitionType,timeottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,more)

函数接收4个参数,你可能比较奇怪,为啥前面MyApp里只传入了一个参数,看过lua语法的应该知道,lua中函数的参数可以少写或者多写,少写得话,对应位置的参数会赋值nil,多写的话会被省略,所以

)等同于self:enterScene(ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,nil,nil)
  • sceneName是你要跳转的场景名

  • args是跳转场景是传给该场景类构造函数的参数,args需要是一个table

  • transitionType是场景切换的过渡动画类型

  • time过渡时间

  • more是过渡效果附加参数

在基本的使用中,你只需要传入sceneName即可:

localscenePackageName=self.packageRoot..".scenes."..sceneName
localsceneClass=require(scenePackageName)
localscene=sceneClass. new (unpack(checktable(args)))

这三句的意思是确定该场景所在的包名,引入该场景的文件生成一个该场景的实例。

通过看scenePackageName的赋值你会看出,使用enterScene方法跳转场景是有限制的,该场景的定义文件一定要在root/scenes/这个文件夹中,比如root/scenes/MainScene.lua。

随后,调用display的转换场景方法来转换场景

display.replaceScene(scene,serif; font-size:14px"> 同样的逻辑你可以看下createView这个方法,这个方法是创建一个定义文件在root/views/这个目录下的view

5.用args给要切换到的场景传参

前面说过args传参需要是一个table类型,比如给MainScene传参可以这样写

ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; font-family:Consolas,{10,20})

那么如何在MainScene中接收这两个参数呢?需要这样改写MainScene的ctor定义

MainScene:ctor(arg1,arg2)

此时,arg1 = 10, arg2 = 20

好了,关于MyApp的内容就讲到这里了,本篇结束!

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...