从零开始学做游戏开发
一直在用java从事android应用开发,用空余时间自学了c++,有了c++基础后今天开始学习cocos2d-x,cocos2d-x环境的搭建就不罗嗦了,直接进入正题。从新建的cococs2d项目开始说起吧。
(注:版本cocos2d-x,新建项目时要使用命令行进行创建。)
这里一个简便的方法就是采用批处理,以后创建新项目就不必那么麻烦了。
@echo off
set /p projectName=请输入项目名称:
if "%projectName%"=="" goto inputError
set /p packageName=请输入包标示名:
if "%packageName%"=="" goto inputError
create_project.py -project %projectName% -package %packageName% -language cpp
pause
exit
:inputError
@echo 项目名称或者包标示名不能为空!
pause
将这个复制到文本后改成.bat文件,放到cocos2d的\tools\project-creator目录下。双击按操作进行便可以新建项目了。
#include "main.h"
#include "AppDelegate.h"
#include "CCEGLView.h"
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;
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
eglView->setViewName("MyFirstApp");
eglView->setFrameSize(480, 320);
return CCApplication::sharedApplication()->run();
}
AppDelegate:
引擎运行的委托对象,负责游戏启动、关闭的事件处理工作。针对智能手机平台的程序占用前端和进入后台事件的处理工作。
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
eglView->setViewName("MyFirstApp");
eglView->setFrameSize(480, 320);
设置了窗体大小和名字。
CCApplication::sharedApplication()->run();
开始进行绘制。
在AppDelegate.cpp里是一些事件的处理。
#include "AppDelegate.h"
#include "HelloWorldScene.h"
USING_NS_CC;
AppDelegate::AppDelegate() {
}
// AppDelegate析构函数,用于释放内存,游戏结束时在此进行清理操作。
AppDelegate::~AppDelegate()
{
}
// 该函数在程序启动之后调用,在此进行游戏的初始化。
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
// turn on display 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;
}
// 该函数在程序进入后台时调用,用于暂停游戏。
void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->stopAnimation();
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
// 该函数在程序由后台回到前台时调用,用于恢复游戏。
void AppDelegate::applicationWillEnterForeground() {
CCDirector::sharedDirector()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
以上就是cocos2d-x引擎的框架大致结构。