撸到猴年马月之保卫萝卜(二)helloworld分析

1 6个文件
        Classes
        AppDelegate.h
        AppDelegate.cpp
        HelloWorldScene.h
        HelloWorldScene.cpp
        
        win32
        main.cpp
        main.h
        
2 AppDelegate.h/AppDelegate.cpp
        
        class AppDelegate : private cocos2d::CCApplication
        一个不常见的private继承
        The reason for implement as private inheritance is
        to hide some interface call by CCDirector.
        
        除去构造和析构函数共有3个方法
        virtual bool applicationDidFinishLaunching();
        virtual void applicationDidEnterBackground();
        virtual void applicationWillEnterForeground();
        
        1> applicationDidFinishLaunching();
bool AppDelegate::applicationDidFinishLaunching() {
	// initialize director
   	// 初始化游戏引擎控制器CCDirector,以便启动游戏引擎
        CCDirector* pDirector = CCDirector::sharedDirector();
        CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

        pDirector->setOpenGLView(pEGLView);
                
        // turn on display FPS
        //启用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
        //创建helloworld场景
        CCScene *pScene = HelloWorld::scene();

        // run
        pDirector->runWithScene(pScene);

        return true;
}
        FPS一般60以上流程
        
        2> applicationDidEnterBackground()
        CCDirector::sharedDirector()->stopAnimation();
        当进入后台时调用
        
        3> applicationWillEnterForeground()
        CCDirector::sharedDirector()->startAnimation();
        当从后台回到前台时调用
        
3 HelloWorldScene.cpp/HelloWorldScene.h
 class HelloWorld : public cocos2d::CCLayer
 {
 	public:
        	// Here's a difference. Method 'init' in cocos2d-x returns bool,
                //instead of returning 'id' in cocos2d-iphone
                virtual bool init();  

                // there's no 'id' in cpp, so we recommend returning the class instance pointer
                static cocos2d::CCScene* scene();
                
                // a selector callback
                void menuCloseCallback(CCObject* pSender);
                
                // implement the "static node()" method manually
                CREATE_FUNC(HelloWorld);
 };
        1> CCScene* HelloWorld::scene()
        在Cocos2d中,在层下设置一个创建场景的静态函数是一个常用的技巧。
        我们为helloworld层编写了CCLayer的一个子类,在子类中为层添加各种精灵或者逻辑处理代码。
CCScene* HelloWorld::scene()
{
	// 'scene' is an autorelease object
        CCScene *scene = CCScene::create();
                
        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();

        // add layer as a child to scene
        scene->addChild(layer);

        // return the scene
        return scene;
}
        2> bool HelloWorld::init()
        代码分为4个部分:
        --1 对父类初始化
if ( !CCLayer::init() )
{
    return false;
}
        --2 创建一个菜单并添加到层
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
        --3 创建helloworld标签并添加到层
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                        origin.y + visibleSize.height - pLabel->getContentSize().height));
this->addChild(pLabel, 1);
        --4 创建显示"helloworld.png"的精灵并添加到层
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(pSprite, 0);
我们试着换一下图片和文字
 

相当简单啊,修改这两句就行了。看来引擎果然很方便啊。
CCLabelTTF* pLabel = CCLabelTTF::create("My Radish", "Arial", 24);
CCSprite* pSprite = CCSprite::create("Mytest.png");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值