创建一个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\bin
F:\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 .
2016/03/19 13:18 ..
2015/11/03 11:15 189 cocos
2015/10/21 17:07 39 cocos.bat
2015/11/04 19:11 31,093 cocos.py
2016/03/19 13:18 28,748 cocos.pyc
2015/11/03 11:15 1,862 cocos2d.ini
2015/10/21 17:07 25,013 cocos_project.py
2016/03/19 13:18 26,729 cocos_project.pyc
2015/10/21 17:07 15,726 cocos_stat.py
2016/03/19 13:18 14,257 cocos_stat.pyc
2015/10/21 17:07 2,409 install.py
2015/10/21 17:07 4,677 MultiLanguage.py
2016/03/19 13:18 4,824 MultiLanguage.pyc
2015/10/21 17:07 87,798 strings.json
2015/10/21 17:07 7,056 utils.py
2016/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.hellogam
e'。
> 替换 iOS 工程的 Bundle ID,'org.cocos2dx.hellocpp' 替换为 'com.hanhan.hellogam
e'。
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
#include
// 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 platforms
void AppDelegate::initGLContextAttrs()
{
//set OpenGL context attributions,now can only set six attributions:
//red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
// If you want to use packages manager to install more packages,
// don't modify or remove this function
static 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, 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 too
void 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 again
void 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","fonts/Marker Felt.ttf", 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
}