[返回首页 ] 版权声明
加入收藏 网站地图
关于我们 联系我们
   最新文章
   推荐文章
   点击排行
Cocos2dx笔记
Cocos2dx第一个程序
来源:霜叶工作站 点击次数: 发布时间:2016-05-21 19:23:18


第一个程序分析
window下

1.入口:
main.cpp

//创建程序实例,主程序循环
   
AppDelegate app;
   
return Application::getInstance()->run();


2.渲染设置
//设置渲染设置的一些属性
void AppDelegate::initGLContextAttrs()
{
    //6个可以设置的属性 red,green,blue,alpha,depth,stencil
    GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
    GLView::setGLContextAttrs(glContextAttrs);
}


3.
//应用启动完成
bool AppDelegate::applicationDidFinishLaunching()
{
    //导演
    auto director = Director::getInstance();

 //创建渲染视图
    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("Test", Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else
        glview = GLViewImpl::create("Test");
#endif
        director->setOpenGLView(glview);
    }

    //显示FPS等信息
    director->setDisplayStats(true);

    //设置帧率
    director->setAnimationInterval(1.0 / 60);

    //设置分辨率
    glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
   
 //获取屏幕宽高,设置合适的分辨率缩放因子
 //glview->setFrameSize(1024, 768);
 Size frameSize = glview->getFrameSize();
    if (frameSize.height > mediumResolutionSize.height)
    {       
        director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
    }
    else if (frameSize.height > smallResolutionSize.height)
    {       
        director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
    }
    else
    {       
        director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
    }

 //empty
    register_all_packages();

    //创建一个场景
    auto scene = HelloWorld::createScene();

    //运行这个场景
    director->runWithScene(scene);

    return true;
}


4.
//程序切到后台调用
void AppDelegate::applicationDidEnterBackground()
{
 //停止所有的动画
    Director::getInstance()->stopAnimation();

    //停止所有的声音
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

//程序切换到前台调用
void AppDelegate::applicationWillEnterForeground()
{
 //恢复所有的动画
    Director::getInstance()->startAnimation();

    //恢复所有的声音
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}


5.创建一个场景层
1)头文件
class HelloWorld : public cocos2d::Layer
{
public:
 //外部创建实例时调用
    static cocos2d::Scene* createScene();

 //初始化
    virtual bool init();
   
    //一个回调
    void menuCloseCallback(cocos2d::Ref* pSender);
   
    //实现create自身实例的static函数,并主动调用init函数
    CREATE_FUNC(HelloWorld);
};


6.
//初始化
bool HelloWorld::init()
{
    //基类先调
    if ( !Layer::init() )
    {
        return false;
    }
    //可视区域的大小
    Size visibleSize = Director::getInstance()->getVisibleSize();
 //可视区域的起点坐标
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    //创建关闭按钮
    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);

 //添加按钮层,层级为1
    this->addChild(menu, 1);

 //创建文本标签
    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));

    //添加图片,层级0
    this->addChild(sprite, 0);
   
    return true;
}


7.
//关闭按钮响应事件
void HelloWorld::menuCloseCallback(Ref* pSender)
{
 //退出程序
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}