zoukankan      html  css  js  c++  java
  • cocos2d-x 下的HelloWorld

     参考了沈大海的博客:

    为什么要定义windows平台

    因为在不同平台有不同的程序入口实现方式,windos平台有main.hmain.cpp,android平台有入口的Activity,iso平台有main.m,

    但对于各平台的入口差异在cocos2d-x中做了完美的一致化处理,暂且不管是如何进行的,我们只需要基于一致的引擎入口进行开发就好了,

    对于cocos2d-x引擎的入口我们定义为AppDelegate.hAppDelegate.cpp看这里面的方法有三个。

    如下图:windows下工程的入口和引擎入口:

    这几个方法会在各平台应用程序状态改变时候自动回调。

    而状态的改变在不同平台是有差异的,cocos2d-x定义cocos2d::CCApplication类统一了各平台的差异,在cocos2d-x中只有1个窗口在运行(符合移动平台,但pc平台原本不是这样),窗口在分配空间,加载完成,被覆盖,被恢复时候会自动回调CCApplication中的函数,因此CCApplication在不同的平台,有不同的实现cpp,

    (有兴趣同学可以阅读cocos2ds/platform下面的源代码,从cocos2d::CCApplication.h开始)

    看注释就已经很明白了:

    和android的每个activity的活动周期差不多

     

    #ifndef  _APP_DELEGATE_H_
    #define  _APP_DELEGATE_H_
    
    #include "cocos2d.h"
    
    /**
    @brief    The cocos2d Application.
    
    The reason for implement as private inheritance is to hide some interface call by CCDirector.
    */
    class  AppDelegate : private cocos2d::CCApplication
    {
    public:
        AppDelegate();
        virtual ~AppDelegate();
    
        /**
        @brief    Implement CCDirector and CCScene init code here.
        @return true    Initialize success, app continue.
        @return false   Initialize failed, app terminate.
        */
        virtual bool applicationDidFinishLaunching();
    
        /**
        @brief  The function be called when the application enter background
        @param  the pointer of the application
        */
        virtual void applicationDidEnterBackground();
    
        /**
        @brief  The function be called when the application enter foreground
        @param  the pointer of the application
        */
        virtual void applicationWillEnterForeground();
    };
    
    #endif // _APP_DELEGATE_H_
    

     

      

     

    ----------------------------------------------------------------------------------------------------------------------------------

    在这几个方法中,就可以做我们要做的事情了。

    applicationDidFinishLaunching启动完成

    加载游戏,播放音乐

    applicationDidEnterBackground进入背景

    音乐暂停,游戏暂停

    applicationWillEnterForeground恢复窗口

    音乐继续,游戏继续

    可能,你会想,如果应用退出,我想保留游戏数据如何?

    试者找找ccApplication中还有哪些方法。

     

    bool AppDelegate::applicationDidFinishLaunching() {
        // initialize director 初始化导演,他是引擎的老大,单例模式
        CCDirector* pDirector = CCDirector::sharedDirector();
    
        CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
    	//绑定opengles窗口,可见,我们可以自定义openGLView
        pDirector->setOpenGLView(pEGLView);
    	CCSize frameSize = pEGLView->getFrameSize();
    
        // Set the design resolution
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
        pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionShowAll);
    #else
        pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder);
    #endif
    
        
        vector<string> searchPath;
    
        // In this demo, we select resource according to the frame's height.
        // If the resource size is different from design resolution size, you need to set contentScaleFactor.
        // We use the ratio of resource's height to the height of design resolution,
        // this can make sure that the resource's height could fit for the height of design resolution.
    
        // if the frame's height is larger than the height of medium resource size, select large resource.
    	if (frameSize.height > mediumResource.size.height)
    	{
            searchPath.push_back(largeResource.directory);
    
            pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
    	}
        // if the frame's height is larger than the height of small resource size, select medium resource.
        else if (frameSize.height > smallResource.size.height)
        {
            searchPath.push_back(mediumResource.directory);
            
            pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));
        }
        // if the frame's height is smaller than the height of medium resource size, select small resource.
    	else
        {
            searchPath.push_back(smallResource.directory);
    
            pDirector->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
        }
    
    
        // set searching path
        CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
    	
        // turn on display FPS
        pDirector->setDisplayStats(true);
    
        // set FPS. the default value is 1.0/60 if you don't call this
    	//设置FPS 在cocos2d-x 启动后内部封装了FPS的逻辑,虽然helloWorld图片没变化,但其实一直在重绘。
        pDirector->setAnimationInterval(1.0 / 60);
    
        // create a scene. it's an autorelease object  创建一个场景
        CCScene *pScene = HelloWorld::scene(); 
    
        // run 显示这个场景到窗口,必然所有的绘制在场景中定义的
        pDirector->runWithScene(pScene);
    
        return true;
    }
    

     

     

     

    void HelloWorld::menuCloseCallback(CCObject* pSender)
    {
    	CCLog("click the menu");
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    #else
        CCDirector::sharedDirector()->end();
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        exit(0);
    #endif
    #endif
    }
    

      

    总结:

    Director启动一个scene。

    2 addChild

    回调函数

    4 CCLog

     

     

  • 相关阅读:
    zstack(一)运行及开发环境搭建及说明(转载)
    ZStack深度试用:部署、架构与网络及其与OpenStack的对比
    saltops 安装及相关环境安装
    windows下安装rabbitmq的步骤详解
    SQLServer就更新并发处理的解决方案及测试!
    mysql 8.0.20安装教程,mysql详细安装教程
    .NET中的简单的并行
    四、VSCode调试vue项目
    三、使用VSCode配置简单的vue项目
    二、windows下搭建vue开发环境+IIS部署
  • 原文地址:https://www.cnblogs.com/aosting/p/3438869.html
Copyright © 2011-2022 走看看