zoukankan      html  css  js  c++  java
  • cocos2d-x游戏开发系列教程-超级玛丽06-CMGameScene

    背景

    在CMMenuScene中,当用户点击开始游戏时,导演让场景进入到CMGameScene


    头文件

    class CMGameScene : public cocos2d::CCLayer,public CMReceiver
    {
    public:
    	// there's no 'id' in cpp, so we recommend returning the class instance pointer
    	static cocos2d::CCScene* CreateGameScene();
    
    private:
        // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
        virtual bool init();     
        
        // a selector callback
        void menuCloseCallback(CCObject* pSender);
        
        // implement the "static node()" method manually
        CREATE_FUNC(CMGameScene);
    
    	void OnMsgReceive( int enMsg,void* pData,int nSize );
    	void OnCallPerFrame(float dt);
    
    	void InitControlUI();
    	void OnMenuLeftKeyCallBack(CCObject *pSender);
    	void OnMenuRightKeyCallBack(CCObject *pSender);
    	void OnMenuJumpKeyCallBack(CCObject *pSender);
    	void OnMenuFireKeyCallBack(CCObject *pSender);
    
    	enum 
    	{
    		enTagMap,
    		enTagMenu,
    	};
    
    	enum 
    	{
    		enTagLeftKey,
    		enTagRightKey,
    		enTagJumpKey,
    		enTagFireKey,
    	};
    
    	void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
    	void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
    	void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
    	void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);
    
    
    };

    OnMenu系列函数是响应按钮

    ccTouches系列函数是触摸响应函数

    onMsgReceive是自定义的消息处理函数

    OnCallPerFrame是定时刷新处理函数

    init函数则是该scene创建时,被回调的初始化函数


    init函数

    bool CMGameScene::init()
    {
    	do 
    	{
    		//super init first
    		if (CCLayer::init()==NULL)
    		{
    			return false;
    		}
    
    		CMGameMap* pGameMap = CMGameMap::CreateGameMap("MarioMap1.tmx");
    		CC_BREAK_IF(pGameMap==NULL);
    		pGameMap->setPosition(ccp(0,96));
    		addChild(pGameMap,enZOrderBack,enTagMap);
    		
    		//注册Update函数
    		this->schedule(schedule_selector(CMGameScene::OnCallPerFrame));
    
    
    
    		InitControlUI();
    
    		return true;
    	} while (false);
    	CCLog("Fun CMGameScene::init Error!");
    	return false;
    }

    在init函数里,主要的工作:

    1)创建背景地图

    2)注册定时回调函数

    3)初始化控件

    具体的细节,大家可以下载代码看,在这里大家了解框架即可。


    OnCallPerFrame(float dt)

    游戏的逻辑都在OnCallPerFrame中,代码如下:

    void CMGameScene::OnCallPerFrame(float dt)
    {
    	do 
    	{
    		CMGameMap* pMap = dynamic_cast<CMGameMap*>(getChildByTag(enTagMap));
    		CC_BREAK_IF(pMap==NULL);
    		pMap->OnCallPerFrame(dt);
    		
    		//CCLog("TileType = %d",pMap->HeroPosToTileType(pHero->getPosition()));
    		//CCLog("HeroPosX=%f	HeroPosY=%f",pHero->getPositionX(),pHero->getPositionY());
    		return;
    	} while (false);
    	CCLog("fun CMGameScene::Update Error!");
    }
    

    从以上代码可以看出,游戏的逻辑,最终还是转到了CMGameMap这个地图类中。

    所以CMGameScene只是个框,真正处理整个游戏逻辑的还是CMGameMap类


  • 相关阅读:
    .JS replace方法替换所有字符
    .net framework 4.0,结果还是失败,出现HRESULT 0xc8000222错误代码
    用PowerDesigner15自动将数据库里的表生成ER图
    C#对JSON数据格式的处理
    Type of conditional expression cannot be determined because there is no implicit conversion between 'Common.EnumType.EnumGender' and '<null>'
    如何在string.Format方法中输出大括号({})
    网架构学习笔记
    c#实现javascript中函数escape解码
    Solon 开发,八、注入依赖与初始化
    Solon 开发,七、自定义注解开发汇总
  • 原文地址:https://www.cnblogs.com/new0801/p/6177218.html
Copyright © 2011-2022 走看看