[Cocos2d-x for WP8学习笔记] HelloWorld结构分析

先来看一下目录结构:

[Cocos2d-x for WP8学习笔记] HelloWorld结构分析

Assets:游戏资源文件,图片音频等,Resource文件夹也有类似功能

include:用于放置游戏头文件

Shaders:渲染器着色器文件(大雾)

cocos2dorig.cpp/.h:Direct3D游戏默认入口,默认文件名和项目名相同,在Cocos2dx中,经由这里转而启动AppDelegate

//WP8Direct3D游戏默认启动入口
IFrameworkView^ Direct3DApplicationSource::CreateView()
{
return ref new PhoneDirect3DAppDemo();
}
[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
auto direct3DApplicationSource = ref new Direct3DApplicationSource();
CoreApplication::Run(direct3DApplicationSource);
return ;
} //Cocos2dx启动入口
IFrameworkView^ Direct3DApplicationSource::CreateView()
{
return ref new cocos2dorig();
}
ref class CCApplicationFrameworkViewSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
{
public:
virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView()
{
return cocos2d::getSharedCCApplicationFrameworkView();
}
};
[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
//auto direct3DApplicationSource = ref new Direct3DApplicationSource();
//CoreApplication::Run(direct3DApplicationSource);
AppDelegate App;
auto frameworkViewSource = ref new CCApplicationFrameworkViewSource();
Windows::ApplicationModel::Core::CoreApplication::Run(frameworkViewSource);
return ;
}

AppDelegate.cpp/.h:

游戏通用入口文件,控制着游戏的生命周期

bool AppDelegate::applicationDidFinishLaunching()

游戏启动后调用这个方法

bool AppDelegate::applicationDidFinishLaunching()
{
// 初始化游戏引擎控制器
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); // 开发阶段可以打开FPS显示以观察流畅度
//pDirector->setDisplayFPS(false); // 设置横竖屏显示
pDirector->setDeviceOrientation(CCDeviceOrientationPortrait); // 设置每秒刷新率即FPS,默认每秒60次
//pDirector->setAnimationInterval(1.0 / 60); // 创建场景
CCScene *pScene = HelloWorld::scene(); // 运行场景
pDirector->runWithScene(pScene); return true;
}

void AppDelegate::applicationDidEnterBackground()

游戏转入后台时进行的操作,一般来说,要将游戏暂停

void AppDelegate::applicationWillEnterForeground()

游戏回到前台时对应的操作

HelloWorldScene.cpp/.h

HelloWorld游戏的场景,在AppDelegate::applicationDidFinishLaunching()中创建并运行。

CCScene* HelloWorld::scene()

场景创建

CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 创建一个空场景
scene = CCScene::create();
CC_BREAK_IF(! scene); // 创建一个HelloWorld层
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer); // 将HelloWorld层添加到场景中
scene->addChild(layer);
} while (); return scene;
}

bool HelloWorld::init()

初始化HelloWorld类

bool HelloWorld::init()
{
bool bRet = false; do
{
//调用父类方法进行初始化
if ( !CCLayer::init() )
{
break;
} //创建文字标签添加到层中
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Times New Roman", );
pLabel->setPosition( ccp(size.width * 0.5, size.height * 0.5) );
pLabel->setColor(ccc3(, , ));
this->addChild(pLabel, ); //创建显示HelloWorld的精灵并添加到层中
CCSprite *b = CCSprite::create("HelloWorld.png");
b->setPosition(ccp(, ));
b->setPosition(ccp(size.width * 0.5, size.height * 0.5));
this->addChild(b); //设置程序可以响应触摸
setTouchEnabled(true); bRet = true;
} while (); return bRet;
}

本文固定链接:[Cocos2d-x for WP8学习笔记] HelloWorld结构分析

上一篇:setTimeout异步


下一篇:Java 使用 Redis | 菜鸟教程