cocos2dx学习笔记(HelloWorld解析)

本文介绍如何使用Cocos2d-x引擎创建一个简单的游戏场景,并实现基本的用户交互操作,包括添加菜单项以退出程序、显示欢迎信息、以及添加 splash screen 功能。教程覆盖了游戏开发的基本流程,从初始化游戏引擎、创建场景到添加用户界面元素,旨在帮助开发者快速入门游戏开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

入口函数AppDelegate.cpp

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
    //设置绘制间隔为1/60每秒
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    //创建helloworld场景
    CCScene *pScene = HelloWorld::scene();

    // run
    //启动引擎
    pDirector->runWithScene(pScene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    //当应用程序将要进入后台时,调用
    CCDirector::sharedDirector()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    //当应用程序回到前台时调用
    CCDirector::sharedDirector()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
HelloWorld.cpp函数

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方法来吧创建的层加到场景之中
    scene->addChild(layer);

    // return the scene
    return scene;
}

// 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);
//    将菜单加入HelloWorld层中
    this->addChild(pMenu, 1);

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

    // add a label shows "Hello World"
    // create and initialize a label
//    创建字体对象Hello World
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
    
    // 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
//    将字体加入HelloWorld层中
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
//创建HelloWorld.png精灵对象
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
//    将HelloWorld.png精灵对象位于正中央
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
//    将HelloWorld.png精灵对象加入HelloWorld层中
    this->addChild(pSprite, 0);
    
    return true;
}

//回调函数,关闭场景
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值