cocos2d-x 下的HelloWorld_weixin_34241036的博客-程序员宅基地

技术标签: 操作系统  游戏  移动开发  

 参考了沈大海的博客:

为什么要定义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

 

 

转载于:https://www.cnblogs.com/aosting/p/3438869.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_34241036/article/details/94563683

智能推荐

Windows下对文件夹下所有图片批量重命名_windows 将文件夹内文件改名-程序员宅基地

import ospath = "E:\\image"filelist = os.listdir(path) #该文件夹下所有的文件(包括文件夹)count=0for file in filelist: print(file)for file in filelist: #遍历所有文件 Olddir=os.path.join(path,file) #原来的文件路径..._windows 将文件夹内文件改名

Ubuntu10.04"No init found. Try passing init= bootarg"解决方案-程序员宅基地

在正常状态下误敲 fsck 命令后,果断悲剧。屏幕错误提示错误显示类似于:mount: mounting /dev/disk/by-uuid/***************************** on /rootfailed: Invalid argumentmount: mounting /sys on /root/sys failed: No such file or directo..._updata bootarg error

智能合约概述-程序员宅基地

原文的翻译,并运行了例子上的合约存储pragma solidity ^0.4.0;contract SimpleStorage { uint storedData; function set(uint x) public { storedData = x; } function get() public constant retur...

使用BCryptPasswordEncoder管理密码-程序员宅基地

官方是推荐我们使用BCryptPasswordEncoder而PasswordEncoder 等已经被废弃了 1.看个简单的例子 @Test public void testPass(){ String pass = "hello"; BCryptPasswordEncoder encode = new BCryptPasswordE

游戏编程入门(3):绘制基本 GDI 图形_c++ gdi游戏-程序员宅基地

接上文 游戏编程入门(2):创建游戏引擎和使用该引擎制作小游戏本篇内容包括:使用Windows图形设备接口绘制图形的基础知识设备环境是什么以及它为什么对GDI图形如此重要如何在Windows中绘制文本和原始图形如何创建一个示例程序,在游戏引擎的环境中演示GDI图形_c++ gdi游戏

随便推点

【ATT】Restore IP Addresses-程序员宅基地

如何避免多个0的情况。。。。 vector<string> restoreIpAddresses(string s) { // Note: The Solution object is instantiated only once and is reused by each test case. vector<strin...

Web实现:伪类事件伪类鼠标悬停效果_设置鼠标悬停状态 伪类_jasmyn518的博客-程序员宅基地

伪类实现的鼠标悬停效果,直接上代码:HTML部分:<!DOCTYPE html><html><head><!--系统内置 start--><script type="text/javascript" src="//qgt-style.oss-cn-hangzhou.aliyuncs.com/commonJSCSS/console.js"></script><!--系统内置 end--> <meta_设置鼠标悬停状态 伪类

css 单行文本和多行文本超出显示省略号_css单行显示de-程序员宅基地

white-space:nowrap; //不换行overflow:hidden; //超出隐藏text-overflow:ellipsis; //隐藏类型,省略号_css单行显示de

本地SD-WAN监视工具有什么作用?_sd-wan的监控管理平台-程序员宅基地

根据Enterprise Management Associates一项调查,SD-WAN平台提供一些本机监视,但是需要辅助监视工具,尤其是在寻找应用程序性能不佳的原因时。SD-WAN技术提供本机监视功能,可以增强网络操作。这是一个主要的卖点,它具有混合WAN连接和直接云连接,但是本地SD-WAN监控不能替代传统的网络监控工具。SD-WAN产品通常具有基于云的控制器,可为SD-WAN在公司的物理WAN上创建的覆盖图提供可视性和管理。大多数SD-WAN控制器控制台都提供报告和仪表板,可以按站点、按应用程序_sd-wan的监控管理平台

Navicat 2059 - Authentication plugin ‘caching_sha2_password‘-程序员宅基地

文章目录前提原因分析解决办法解决步骤第一步:进入Docker容器下的mysql第二步:登录MySQL第三步:修改加密规则第四步:刷新权限第五步:测试前提使用Navicat登录远程服务器的数据库发现,认证不通过。本文介绍修改远程服务器docker容器下的mysql使用的工具Shell、Navicat原因分析mysql8.0版本更新了身份验证,caching_sha2_password之前mysql版本采用,mysql_native_password, 例如mysql5.7解决

oracle笔记---SGA之高速缓存区-程序员宅基地

SGA(system global area)系统全局区域,在内存中分配一份共享内存区域为oracle的一些关联进程运行所共享。如DBWn,PMON。SGA分为有以下几部分组成:1.高速缓存区2.重做日志缓存区3.共享池4.大池5.java池6.流池7.固定SGA高速缓存区高速缓存区的主要作用于缓存从数据文件中读取的数据块。当用户请求数据时,oracle会从高速缓存区中检索,如果检索...

推荐文章

热门文章

相关标签