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

允许我讲些与源码无关的事情 - -!稍后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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值