一. 创建cocos2d 新项目
1. 下载并安装完cocos2d+python后,需要利用CoCos2D下的project-creator来创建新的项目
2. cmd 下到 /cocos2d/cocos2d-x-3.0beta2/tools/project-creator 下,运行命令'python create_project.py' 创建项目
Project Name: 项目名称
Package Name: 包名
Project Path: 保存地址
3. 创建完成后在Project Path看到以下目录结构, 可以看到cocos2dx是跨平台的,制作的游戏可以直接生成在android,ios,windows平台下运行。Class文件夹下保存了所有的类,Resourcs文件夹下保存所有的资源文件,包括图片,字体,声音
点击 TestGame.sln 从VS运行工程
二. Hello cocos2d 解析:
1. 项目文件结构:
可以看到新建的项目自动关联了基本的三个类,"libAudio,libchipmunk,libcocos2d",
2. Main
USING_NS_CC;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app; //运行AppDelegate方法
EGLView eglView;
eglView.init("Hello Cocos2d",800,480); //定义窗口名称及分辨率
return Application::getInstance()->run();
}
3. AppDelegate类
AppDelegate::AppDelegate() {
//构造函数
}
AppDelegate::~AppDelegate()
{
//析构函数
}
//程序启动完成后进入这个方法/函数
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director 初始化导演类
auto director = Director::getInstance();
auto eglView = EGLView::getInstance();
director->setOpenGLView(eglView); //设置OpenGL视图
// turn on display FPS 是否打开FPS
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this 设置帧速率
director->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object 创建一个场景
auto scene = HelloWorld::createScene();
// run 载入该场景
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
//当程序进入后台后执行,比如接电话,按Home....
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->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() {
Director::getInstance()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
运行项目: