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的活动周期差不多

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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中还有哪些方法。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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;
}

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
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

 

 

posted @   aosting  阅读(251)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示