游戏开发学习-Cocos2d-x学习(一) HelloWorld 开始

本文介绍了一位服务器方向程序员转型游戏客户端开发的学习之路,重点讲解了使用Cocos2d-x 2.1.4版本创建游戏场景的过程,包括搭建开发环境、创建项目、初始化场景及添加游戏元素等关键步骤。
本人是一个普通的code,做服务器方向的,但是不甘于跟人一直的打工,想学习一下游戏客户端。我是一个c++新手,虽说大学学习过c++,但是也只是皮毛。就带着自己做过三年Java的基础来学习一下游戏开发框架coco2d-x吧!
我采用的cocos2d-x的版本是cocos2d-x-2.1.4版本,网上有很多教程什么的,但是有时候自己下载的版本和作者的版本不一样,有些实现或者api在现在的版本中没有,所以大家在学习时候一定看看自己的版本,本人就在这里吃了亏。
下载Coco2d-x 在http://www.cocos2d-x.org下载,然后按照官方的教程可以一步一步的安装就可以了。

新建一个工程,可以看到有个AppDelegate.cpp

bool AppDelegate::applicationDidFinishLaunching()  
{  
    // CCDirector 是cocos2d-x中比较重要的类,负载了CCScene的加载,运行,卸载等等操作  
    CCDirector *pDirector = CCDirector::sharedDirector();  
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());  
  
    // 是否显示FPS,就是游戏启动之后右下角的数字  
    pDirector->setDisplayStats(true);  
  
    // 设置FPS  
    pDirector->setAnimationInterval(1.0 / 60);  
  
    //创建一个场景,场景是autoRealse的,autoRealse是和Objective-c中autoRealse是一样的意思  
    CCScene *pScene = HelloWorld::scene();  
<span style="white-space:pre">  </span>  
    // 运行一个场景,这里就是用到了CCDirector,CCDirector是单例的  
    pDirector->runWithScene(pScene);  
  
    return true;  
} 
上面的这一段就是启动一个游戏场景,开始进入游戏了。
接下来就是如何创建一个场景。我们来看看HelloWorld::scene();

class HelloWorld : public cocos2d::CCLayer  
{  
public:  
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)  
    virtual bool init();  
  
    // there's no 'id' in cpp, so we recommend to return the class instance pointer  
    static cocos2d::CCScene* scene();  
      
    // a selector callback  
    void menuCloseCallback(CCObject* pSender);  
  
    // preprocessor macro for "static create()" constructor ( node() deprecated )  
    CREATE_FUNC(HelloWorld);  
};  

这里的HelloWorld是继承自CCLayer表示一个层,然后有一个静态的static coco2d::CCScene* scene(); 这里创建出一个场景,然后在场景里加上CCLayer。这里还有一个void menuCloseCallback(CCObject* pSender);稍后我们会看到。
接下来我们看看HelloWorld的实现。
首先看看静态方法static coco2d::CCScene* scene();

CCScene* HelloWorld::scene()  
{  
    // 创建了一个CCScene,CCScene中的create方法这里先不做研究  
    CCScene *scene = CCScene::create();  
      
    // 创建一个层,create方法是HelloWorld从CCLayer继承得来,和CCScene中的create是一样的,create方法会调用init方法。  
    HelloWorld *layer = HelloWorld::create();  
  
    //将一个层添加到一个场景中  
    scene->addChild(layer);  
  
    // return the scene  
    return scene;  
}  

这里比较重要的是HelloWorld::create(),调用了init方法,属于多态。我们再看看HelloWorld::init()方法。

// on "init" you need to initialize your instance  
bool HelloWorld::init()  
{  
    //////////////////////////////  
    // 1. 父类的初始化方法调用  
    if ( !CCLayer::init() )  
    {  
        return false;  
    }  
  
    /////////////////////////////  
    // 2. 添加一个MenuItem. 这里的ccp是一个宏,创建出一个CCPonitMake.  
  
    // 添加一个图片的按钮  
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
                                        "CloseNormal.png",  
                                        "CloseSelected.png",  
                                        this,  
                                        menu_selector(HelloWorld::menuCloseCallback) );  
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );  
  
    // 创建一个菜单  
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
    pMenu->setPosition( CCPointZero );  
    //将菜单添加到层上  
    this->addChild(pMenu, 1);  
  
    /////////////////////////////  
    // 3. 下面可以自己添加自己的代码了  
  
    // 添加一个label现实一个文字"Hello world"  
    // create (const char* string,const char* fontName,float size)  
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);  
  
    //获取当前窗口的大小  
    CCSize size = CCDirector::sharedDirector()->getWinSize();  
  
    //设置pLabel的位置,  
    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );  
  
    //将pLabel加到当前的layer中  
    this->addChild(pLabel, 1);  
  
    //创建一个精灵类,精灵类是游戏中主要的处理的对象,就是游戏中经常要动得类。  
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");  
  
    //设置精灵类的位置。  
    pSprite->setPosition( ccp(size.width/2, size.height/2) );  
  
    // 将精灵加到当前层中  
    this->addChild(pSprite, 0);  
      
    return true;  
}  

到此我们就在一个场景中创建了一个层,层内有精灵类,有文字,有按钮。这里还有按钮的回调事件句柄没有写;

void HelloWorld::menuCloseCallback(CCObject* pSender)  
{  
    CCDirector::sharedDirector()->end();  
  
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
    exit(0);  
#endif  
}  

 这里就是简单的结束,调用CCDirector的end()方法。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值