二、Cocos2dx之HelloWorld

Cocos2d-x工程中自带helloworld项目,我们就可以通过这个项目来看看cocos2d-x的基本结构。

项目结构:

 

可以看到proj.android,proj.linux等文件夹与平台相关,与开发逻辑相关的即是Classes和Resources文件夹,Classes文件夹放置代码,Resource放置资源(图片、声音、图片配置文件等)。

程序结构:

在win32平台上,就是win32文件夹中的内容和Classes当中的内容,win32文件夹内就是main类,而Classes中的平台配置文件和我们的helloworld场景类。

main类就是win32平台的一个入口类启动程序。

 AppDelegate是应用真正的入口,在这里有平台的适配参数,找到导演类,启动我们的场景。在AppDelegate除了构建函数和析构函数之外,就是处理暂停和重新开始的函数,在applicationDidFinishLaunching中就是处理导演类和场景开始:

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    // 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;
}

代码很容易懂包括设置导演类帧每秒等参数。

最后是用摄像机的runWithScene开始场景。

最后就是HelloWorldScene场景类,构建方法主要是建层等,关键的方法是init,主要是构建场景中的各种元素:

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->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
    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));

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

    /
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);
    
    // position the label on the center of the screen
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    return true;
}

代码也很容易懂,每次创建元素后将其使用this->addchild函数插入,即放入此场景,第二个参数就是
绘制的顺序。其中构建关掉程序的方法中传入了menuCloseCallback方法,即是本类中的menuCloseCallback方法,这样就完成了第一个程序helloworld。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值