Cocos2d-x 3.1 HelloWorld解析

Cocos2d-x 3.1 HelloWorld解析,第1张

概述创建一个Cocos2dx_C++项目 需要先安装 Python cocos2d-x-3.10\tools\cocos2d-console\bin下 用python 打开cocos.py 创建一个项目 python cocos.py new hellogame -p com.hanhan.hellogame -l cpp -d e:\cocos2dxpros\hellogame 下面是实例: /** 创建一个Cocos2dx_C++项目 需要先安装 Python
cocos2d-x-3.10\tools\cocos2d-console\bin下 用python 打开cocos.py
创建一个项目
python cocos.py new hellogame -p com.hanhan.hellogame -l cpp -d e:\cocos2dxpros\hellogame
下面是实例:
/**************************************************************
****************************************************************
*/
--------------Microsoft windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation。保留所有权利。C:\Users\han>f:F:\>cd F:\cocos2d-x-3.10\tools\cocos2d-console\binF:\cocos2d-x-3.10\tools\cocos2d-console\bin>dir 驱动器 F 中的卷是 开发 卷的序列号是 0005-74D0 F:\cocos2d-x-3.10\tools\cocos2d-console\bin 的目录2016/03/19  13:18    <DIR>          .2016/03/19  13:18    <DIR>          ..2015/11/03  11:15               189 cocos2015/10/21  17:07                39 cocos.bat2015/11/04  19:11            31,093 cocos.py2016/03/19  13:18            28,748 cocos.pyc2015/11/03  11:15             1,862 cocos2d.ini2015/10/21  17:07            25,013 cocos_project.py2016/03/19  13:18            26,729 cocos_project.pyc2015/10/21  17:07            15,726 cocos_stat.py2016/03/19  13:18            14,257 cocos_stat.pyc2015/10/21  17:07             2,409 install.py2015/10/21  17:07             4,677 MultiLanguage.py2016/03/19  13:18             4,824 MultiLanguage.pyc2015/10/21  17:07            87,798 strings.Json2015/10/21  17:07             7,056 utils.py2016/03/19  13:18             5,124 utils.pyc              15 个文件        255,544 字节               2 个目录 108,059,762,688 可用字节F:\cocos2d-x-3.10\tools\cocos2d-console\bin>python cocos.py new hellogame -p com.hanhan.hellogame -l cpp -d e:\cocos2dxpros\hellogame> 拷贝模板到 e:\cocos2dxpros\hellogame\hellogame> 拷贝 cocos2d-x ...> 替换文件名中的工程名称,'HelloCpp' 替换为 'hellogame'。> 替换文件中的工程名称,'HelloCpp' 替换为 'hellogame'。> 替换工程的包名,'org.cocos2dx.hellocpp' 替换为 'com.hanhan.hellogame'。> 替换 Mac 工程的 Bundle ID,'org.cocos2dx.hellocpp' 替换为 'com.hanhan.hellogame'。> 替换 iOS 工程的 Bundle ID,'org.cocos2dx.hellocpp' 替换为 'com.hanhan.hellogame'。F:\cocos2d-x-3.10\tools\cocos2d-console\bin>






下面是helloworld解析
1.main.h


#ifndef __MAIN_H__#define __MAIN_H__#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from windows headers// windows header files:#include <windows.h>#include <tchar.h>// C RunTime header files#include "CCStdC.h"#endif    // __MAIN_H__


2.main.cpp


/*这里是windows的入口文件 不需要做任何更改!*/#include "main.h"#include "AppDelegate.h"#include "cocos2d.h"USING_NS_CC;int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR    lpCmdline,int       nCmdshow){    UNREFERENCED_ParaMETER(hPrevInstance);    UNREFERENCED_ParaMETER(lpCmdline);	/*	创建这个App实例 使其进入消息循环	*/    AppDelegate app;    return Application::getInstance()->run();}



3.AppDelegate.h
#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 Director.*/class  AppDelegate : private cocos2d::Application{public:    AppDelegate();    virtual ~AppDelegate();    virtual voID initGLContextAttrs();    /**    @brIEf    Implement Director and Scene 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_






4.AppDelegate.cpp
#include "AppDelegate.h"#include "HelloWorldScene.h"USING_NS_CC;static cocos2d::Size designResolutionSize = cocos2d::Size(480,320);static cocos2d::Size smallResolutionSize = cocos2d::Size(480,320);static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024,768);static cocos2d::Size largeResolutionSize = cocos2d::Size(2048,1536);AppDelegate::AppDelegate() {}AppDelegate::~AppDelegate() {}//if you want a different context,just modify the value of glContextAttrs//it will takes effect on all platformsvoID AppDelegate::initGLContextAttrs(){    //set OpenGL context attributions,Now can only set six attributions:    //red,green,blue,Alpha,depth,stencil    GLContextAttrs glContextAttrs = {8,8,24,8};    GLVIEw::setGLContextAttrs(glContextAttrs);}// If you want to use packages manager to install more packages,// don't modify or remove this functionstatic int register_all_packages(){    return 0; //flag for packages manager}bool AppDelegate::applicationDIDFinishLaunching() {    // 初始化 导演类 Director    auto director = Director::getInstance();	//|获取OpenGl的视图    auto glvIEw = director->getopenGLVIEw();    if(!glvIEw) {#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_liNUX)        glvIEw = GLVIEwImpl::createWithRect("hellogame",Rect(0,designResolutionSize.wIDth,designResolutionSize.height));#else        glvIEw = GLVIEwImpl::create("hellogame");#endif		//设置OPenGl的视图        director->setopenGLVIEw(glvIEw);    }    // 是否显示FPS帧率    director->setdisplayStats(true);    // set FPS. the default value is 1.0/60 if you don't call this    director->setAnimationInterval(1.0 / 60);    // Set the design resolution    glvIEw->setDesignResolutionSize(designResolutionSize.wIDth,designResolutionSize.height,ResolutionPolicy::NO_border);    Size frameSize = glvIEw->getFrameSize();    // if the frame's height is larger than the height of medium size.    if (frameSize.height > mediumResolutionSize.height)    {                director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height,largeResolutionSize.wIDth/designResolutionSize.wIDth));    }    // if the frame's height is larger than the height of small size.    else if (frameSize.height > smallResolutionSize.height)    {                director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height,mediumResolutionSize.wIDth/designResolutionSize.wIDth));    }    // if the frame's height is smaller than the height of medium size.    else    {                director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height,smallResolutionSize.wIDth/designResolutionSize.wIDth));    }    register_all_packages();    // 创建一个场景对象    auto scene = HelloWorld::createScene();    // 运行这个场景    director->runWithScene(scene);    return true;}// This function will be called when the app is inactive. When comes a phone call,it's be invoked toovoID AppDelegate::applicationDIDEnterBackground() {    Director::getInstance()->stopAnimation();    // if you use SimpleAudioEngine,it must be pause    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();}// this function will be called when the app is active againvoID AppDelegate::applicationWillEnterForeground() {    Director::getInstance()->startAnimation();    // if you use SimpleAudioEngine,it must resume here    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();}



5.HelloWorldScene.h
/*场景类定义*/#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"//|继承自 Layer层类class HelloWorld : public cocos2d::Layer{public:	//|创建场景方法    static cocos2d::Scene* createScene();	//|初始化    virtual bool init();        //|关闭菜单(按钮) 回调函数    voID menuCloseCallback(cocos2d::Ref* pSender);    	/*	根据 CREATE_FUN()这个宏 会在 本类的 类成员中加一个 static create()这个静态成员函数 (用于创建这个类)	*/    // implement the "static create()" method manually    CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__



6.HelloWorldScene.cpp
#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){    // 创建场景    auto scene = Scene::create();        //创建层    auto layer = HelloWorld::create();    // 把层放在场景    scene->addChild(layer);    // return the scene    return scene;}//|初始化层bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }        Size visibleSize = Director::getInstance()->getVisibleSize();    Vec2 origin = Director::getInstance()->getVisibleOrigin();    /////////////////////////////    // 2. add a menu item with "X" image,which is clicked to quit the program    //    you may modify it.   //|创建关闭菜单项(按钮)    auto closeItem = MenuItemImage::create(                                           "Closenormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));    //设置位置	closeItem->setposition(Vec2(origin.x + visibleSize.wIDth - closeItem->getContentSize().wIDth/2,origin.y + closeItem->getContentSize().height/2));    // 创建菜单    auto menu = Menu::create(closeItem,NulL);	//|设置位置    menu->setposition(Vec2::ZERO);	//添加到层    this->addChild(menu,1);    /////////////////////////////    // 3. add your codes below...    // add a label shows "Hello World"    // create and initialize a label    //创建标签    auto label = Label::createWithTTF("Hello World","Fonts/Marker Felt.ttf",24);        //设置位置    label->setposition(Vec2(origin.x + visibleSize.wIDth/2,origin.y + visibleSize.height - label->getContentSize().height));    //添加到层    this->addChild(label,1);    //创建精灵    auto sprite = Sprite::create("HelloWorld.png");    //设置位置    sprite->setposition(Vec2(visibleSize.wIDth/2 + origin.x,visibleSize.height/2 + origin.y));   //添加到层    this->addChild(sprite,0);    // 增加一个自己的Label	auto label2 = Label::createWithTTF("QiuYuhan`s First Game",24);	label2->setposition(Vec2(label2->getContentSize().wIDth/2,100));	this->addChild(label2,1);    return true;}//事件处理函数voID HelloWorld::menuCloseCallback(Ref* pSender){	//关闭应用    Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}
总结

以上是内存溢出为你收集整理的Cocos2d-x 3.1 HelloWorld解析全部内容,希望文章能够帮你解决Cocos2d-x 3.1 HelloWorld解析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1078690.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存