【Cocos2d-x】Cocos2d-x参考案例源码解析之三:HelloWorld

2023-05-16

允许我讲些与源码无关的事情 - -!稍后K我

由于cocos2dX3.0快出来了,官网上说放弃objective c风格,本屌学的是C++,所以真是太高兴了,所以放慢的源码解析 - -(其实给自己找理由,最多变下类名- -),决定直接讲掉HelloWorld进行实际开发。

好了,其实这几天也没闲,游戏开发如果不想window MFC有控件的话,单纯代码cocos2dX代码写控件的话那是很吓人的,你会写上几百行只为实现一个按钮效果。好了,我是要引出CocoStuido工具,这个工具很强大,触控集团开发的,有场景编辑器,UI编辑器,动作动画编辑了,这样我们程序猿就不必纠结界面的设计布局,交给工具做吧,我们只要考虑逻辑了,是不是大大简化了程序员的工作呢。

关于CocoStudio下载:http://www.cocostudio.org/

工具使用介绍:

UI编辑器用户指引:  http://www.cocoachina.com/bbs/read.php?tid=161578&keyword=CocoStudio%BD%CC%B3%CC
Action编辑器用户指引: http://www.cocoachina.com/bbs/read.php?tid=161600&keyword=CocoStudio%BD%CC%B3%CC
Scene编辑器用户指引:  http://www.cocoachina.com/bbs/read.php?tid=161606&keyword=CocoStudio%BD%CC%B3%CC


-------------------------------------------------------好了,我们来讲HelloWorld-------------------------------------------------------------------

当然,还是从Main入口开始看

 

eglView->setFrameSize(2048, 1536);
    // The resolution of ipad3 is very large. In general, PC's resolution is smaller than it.
    // So we need to invoke 'setFrameZoomFactor'(only valid on desktop(win32, mac, linux)) to make the window smaller.
    eglView->setFrameZoomFactor(0.4f);

设置了屏幕大小为2048,1536;这里也可以通过Api获取当前设备的宽高然后赋予,这里你看到有个setFrameZoomFactor(0.4f),这个上面解释是Ipad3的分辨率非常大,调整分辨率使用win32,mac,linux设备;主要为了实现高清


下面看AppDelegate.cpp


bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);
	CCSize frameSize = pEGLView->getFrameSize();

    // 设置设计分辨率
	// 下一章我会讲为什么需要这个setDesignResolutionSize函数
	// 这里调用了AppMacros.h文件里的定义,采用的是条件编译(不知道条件编译的百度或者留言我再讲吧)来  根据不同设备定义不同的 设置设计分辨率 大小
#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;

	// 在这个演示中,我们选择资源根据帧的身高。
	// 如果资源大小不同设计分辨率大小,你需要用contentScaleFactor设置调整。
	// 我们使用资源的高度比分辨率高度设计的
	// 这可以确保资源的高度能适应的设计分辨率高度。

	// 如果屏幕的高度大于媒体资源高度的大小,选择大的资源。
	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));
    }


    // 字母上理解设置搜索资源路径
    CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
	
    // 开启 FPS
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    CCDirector::sharedDirector()->stopAnimation();
	// 进入后台调用
    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    CCDirector::sharedDirector()->startAnimation();
	// 退出后台进入活动状态前
    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}




applicationDidEnterBackground()和applicationWillEnterForeground()

你可以分别打断点F9,然后进入调试状态F5,启动界面后缩小对话框 和恢复分别会进入断点就可以理解了。

CCScene *pScene = HelloWorld::scene();
此句调用HelloWorld 的静态scene函数,HelloWorld派生与cocos2d::CCLayer

下面看HelloWorld.cpp

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
	//调用基类,从上面的官方注释的英文可以看出基类的Create里有new操作,并且加入了自动释放池(关于自动释放池参考OC)中
    CCScene *scene = CCScene::create();
	// 'layer' is an autorelease object
	// 这里创建我们的图层,加入了自动释放池,进行自动管理不用担心内存泄露
	// 这里你看HelloWorld的头文件并没有定义静态函数create()
	// 这是为什么呢? 但是你看下头文件中有这么一个东西
	// CREATE_FUNC(HelloWorld);
	// 注释说手动实施静态节点方法
	// 其实是为了统一定义了一个宏
	// 鼠标点击在CREATE_FUNC上右键,选择转到定义或者F12
    // 可以看到和基类的CCScene::create()基本一样的,里面调用了init方法
	// 而这个init方法,实现这个图层的功能,我们可以将相关的实现代码放在这里
	// 当然,这是一个虚函数,返回一个bool
   
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
	// 将图层加入到场景中
    scene->addChild(layer);

    // return the scene
    return scene;
}


鼠标点击在create上,右键选择转到定义,或者点击图片右上角的GO,或者按F12查看create

看bool HelloWorld::init()

{
    //
    // 1. super init first
	// 先初始化超类
    if ( !CCLayer::init() )
    {
        return false;
    }
    
	// 可视区域大小
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	// 表示可视区域的起点坐标
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
	// 创建菜单项图片 第一个参数代表一般情况下显示的图片,第二个参数代表按下去的时候显示的图片,
	// 第三个参数代表放在那个曾,
	// 第四个参数是回调函数-静态函数,用于响应点击这个图片时需要响应
	//
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
	// 设置菜单项图片位置
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
	// 创建菜单
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	// 设置菜单位置
    pMenu->setPosition(CCPointZero);
	// 菜单加入到图层
    this->addChild(pMenu, 1);

    /
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    // 创建Lable
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);
    
    // position the label on the center of the screen
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

	// 这里本人弄了半天 不理解visibleSize.width/2  为什么除以2,后来才知道- -||
	// 原因是C++的坐标是以左上角为原点,如果设置图片的话以图片左上角做参考位置来设置
	// 而cocos2dX采用OPenGL左下角为坐标原点,向上增加,向右增加,
    // 默认锚点为0.5,0.5,即质点位置,以锚点为图片的参考点
	// 这个设置精灵的图片  位置是相对于以精灵的中心点(锚点)位置进行设置的
    // position the sprite on the center of the screen

	pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);

	
    return true;
}




刚接触Cocos2d-x,如有错误请大家纠正,共同学习进步 

 编程QQ群:160296200

  本篇博客出Leon,转载请注明出处: http://blog.csdn.net/leoncoder/article/details/12791143
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【Cocos2d-x】Cocos2d-x参考案例源码解析之三:HelloWorld 的相关文章

  • 手把手教你实现window图片爬虫(一)

    第一篇 xff1a 爬虫设计思路及原理 刚听说爬虫时 xff0c 估计很多人觉得很神奇 xff0c 是什么赋予了它生命力做到在网络上到处爬取的呢 xff1f 等我说完你会恍然大悟 xff0c 其实并没有多高深的技术 xff0c 人人都可以写
  • IDEA PermGen space内存溢出

  • MySql/MariaDB 中文乱码问题

    今天装了MySql 发现换名字了啊 xff0c 结果用的时候出现了乱码 xff0c MariaDB会出现中文乱码 xff0c 解决方法 xff1a 1 xff09 查看数据库编码的方法 MariaDB itat hibernate gt s
  • Synchronized锁详解

    在Java中 xff0c synchronized锁可能是我们最早接触的锁了 xff0c 在 JDK1 5之前synchronized是一个重量级锁 xff0c 相对于juc包中的Lock xff0c synchronized显得比较笨重
  • Windows10开机自动启动VirtualBox(以无界面方式启动)-虚拟机

    以下操作 xff0c 亲测win10下可以开机自启动 xff0c 但未能实现以无界面方式启动 windows 开机启动 虚拟机 以无界面方式启动 桌面上出现了一个快捷方式 右键快捷方式 属性 在目标中添加 type headless 表示
  • ardupilot EKF3核心算法《EKF算法推导》

    目录 文章目录 目录 摘要 1 EKF算法推导 摘要 本节主要记录ardupilot EKF3核心算法 EKF算法推导 的过程 欢迎批评指正 1 EKF算法推导
  • java自带的注解@ PostConstruct

    java注解 64 PostConstructor 1 spring项目加载数据字典 64 PostConstruct注解的方法在项目启动的时候执行这个方法 xff0c 也可以理解为在spring容器启动的时候执行 xff0c 可作为一些数
  • 排序算法总结

    原文链接 https mp weixin qq com s HQg3BzzQfJXcWyltsgOfCQ 本文将采取动态图 43 文字描述 43 正确的java代码实现来讲解以下十大排序算法 xff1a 冒泡排序 选择排序 插入排序 希尔排
  • 关于rebase

    场景复现 xff1a 本来要在refund分支上的修改的代码 xff0c 结果由于分支太多写在了queue分支上 如何恢复queue分支到提交之前的版本 xff1f xff1f 1 git log 找到commitid 2 git rese
  • macOS big sur Navicat Premium12.1.15 无法正常启动

    提示信息 xff1a Navicat Premium 因为出现问题而无法打开 错误日志提示 Dyld Error Message dyld Using shared cache 1E362DBC F66C 3135 BCA0 C1BBAE1
  • H5活动页面遇到的坑+微信分享代码

    h5活动页面功能 xff1a 在手机上微信分享 1 上传两张图片 2 播放一个背景音乐 很简单是么 xff1f 那说明你知道的太少了 xff0c 其实里面的坑好多 一下是制作的心路历程 xff1a 坑1 iphone上传照片的时候 xff0
  • mybatis parametertype 多个参数

    一 单个参数 xff1a public List lt XXBean gt getXXBeanList 64 param 34 id 34 String id lt select id 61 34 getXXXBeanList 34 par
  • JS获取地址中的参数

    lt DOCTYPE HTML PUBLIC 34 W3C DTD HTML 4 0 Transitional EN 34 gt lt html gt lt head gt lt title gt 打印 lt title gt lt met
  • less 查看日志,并且搜索

    一 关键字搜索日志 非常实用 1 less catalina out 2 大写字母 xff1a F find的意思 xff0c 并且其实他正在计算行数 直接到达日志最底部 xff0c 也就是最新日志 3 xff1a ctrl 43 c 把上
  • maven配置,以及项目"Dependency 'xxxx‘ not found"解决过程

    maven安装 1 下载maven文件 地址 2 解压好就可以了 xff0c 无需安装 xff0c 3 修改下面配置文件 配置環境变量 xff1a xff08 和配置jdk一样 xff09 检查配置成功没有 xff1a 直接cmd mvn
  • linux修改系统时间

    一 查看和修改Linux的时区1 查看当前时区 命令 xff1a 34 date R 34 1 修改设置Linux服务器时区方法 A 命令 xff1a 34 tzselect 34 依据引导进行选择 二 查看和修改Linux的时间1 查看时
  • Win2012系统忘记密码,修改密码。

    请准备一张相应操作系统版本的光盘 Server2012R2安装光盘ISO 步骤 1在虚拟机的光盘中选择Server2012R2的ISO 并确定 如果是物理机 直接把ISO刻录成光盘 放入光驱即可 2重启服务器 修改启动项从CD ROM启动
  • 防火墙firewall-cmd

    防火墙firewall cmd 一 centos7查看防火墙所有信息 firewall cmd list all 二 centos7查看防火墙开放的端口信息 firewall cmd list ports 三 开放 删除端口号 3 1 开放
  • docker与firewalld冲突解决

    firewall的底层是使用iptables进行数据过滤 xff0c 建立在iptables之上 xff0c 而docker使用iptables来进行网络隔离和管理 xff0c 这可能会与 Docker 产生冲突 当 firewalld 启
  • gradle和gradle wrapper

    wrapper保证了团队中每一个开发者都使用同样版本的Gradle并能使用Gradle进行项目构建 1 Gradle Wrapper 是什么 Gradle Wrapper 由几个文件组成 xff0c 这些文件在你的项目目录中 l gradl

随机推荐