更新时间:2016-05-28 01:34:16浏览次数:1+次
这篇开始先介绍下cocos2dx的项目结构。跟其他的c++项目一样,源码放在classes里面,资源放在resource下面,根据不同的平台,决解方案放在不同的文件夹下,我这里是osx系统,就以osx为例
打开解决方案,首先还是一样找到main函数,这里的main函数很简单,只是创建一个创建一个项目入口,调用api->run()
直接运行进入应用。
打开AppDelegate.cpp
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);
以上这几个是屏幕适配的几个方案,因为是移植到移动端,所以分辨率适配一直是很头疼的问题
然后看 applicationDidFinishLaunching这个函数,注释都有,这里介绍几个常用的东西
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
这个是判断平台的宏定义
glview = GLViewImpl::createWithRect("hello", Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
这个是创建窗口的api,参数分别是,窗口的名称,窗口的大小范围,
// turn on display 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));
}
以下这些则是开始
// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();
// run
director->runWithScene(scene);
创建一个helloworld类场景,调用导演类director函数runWithScene运行这个场景。这个函数之前介绍过;runWithScene( Scene *scene ) 启动游戏,并运行scene场景。本方法在主程序第一次启动主场景的时候调用。还有不懂的请看上一篇介绍。
打开helloworldscene这个类的头文件
只要涉及游戏场景或者层活着精灵的创建,都要继承cocos2dx的类,这里继承的是layer,当然,也可以继承node,scene
class HelloWorld : public cocos2d::Layer
{
public:
声明一个场景类,用于创建场景的时候用,刚的appdelegate调用的就是这个函数作为入口。
static cocos2d::Scene* createScene();
virtual bool init();这里重写cocos2dx的init方法
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);这个是用户的交互事件出发函数
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);这句话,很重要,一定要加,大体的作用时把这个场景加到cocos2dx的内存管理中。销毁这个场景只需要调用cocos2dx的api就可以了
};
在cpp文件里的函数实现
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();创建一个场景
// 'layer' is an autorelease object
auto layer = HelloWorld::create();创建一个layer层
// add layer as a child to scene
scene->addChild(layer);把这个层加到场景中
// return the scene
return scene;把当前这个场景实例返回给调用的地方,然后给导演类加载,让导演类来显示
}
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first这里调用父类的初始化函数,初始化失败则返回false
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.
这里创建一个按钮
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",按钮正常时候显示
"CloseSelected.png",按钮按下的时候显示
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));按钮的触发事件函数
设置该按钮的坐标,(这里设置的是该按钮所在的菜单的节点的坐标)坐标1??左下角为0,0点开始
传入vec对象,就是设置的坐标的x,y轴(相对于菜单对象的坐标系)
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
创建一个菜单对象,用于存放按钮,只有按钮加进菜单,才能显示出来
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);设置菜单的坐标点ZERO为0,0点
this->addChild(menu, 1);将菜单对象加进该layer层,就可以显示,参数,第二个是把该对象放到第几层,参数越大,显示越靠前
/////////////////////////////
// 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);
// position the label on the center of the screen设置字体坐标
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);把字体加到该layer中(该第二个参数如果一样,那就以创建的顺序显示,越后创建的显示在月前面)
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");创建一精灵对象,传入精灵所要显示的图片
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));设置坐标
// add the sprite as a child to this layer
this->addChild(sprite, 0);将精灵添加到该层,第二个参数0,层级最低。显示在最下面
所有东西都初始化完成后返回true,则显示
return true;
}
这里献上cocos2dx坐标系的结束:http://www.cocos.com/doc/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/v3/coordinate-system/zh.md
运行出来
相关资讯