cocos2d-x在windows平台HelloWorld解析-沈大海cocos2d-x教程之3

来源:互联网 发布:徐宗本院士谈大数据 编辑:程序博客网 时间:2024/04/29 04:08

 

为什么要定义windows平台

 因为在不同平台有不同的程序入口实现方式,如windos平台有main.h和main.cpp,android平台有入口的Activity,iso平台有main.m,

 但对于各平台的入口差异在cocos2d-x中做了完美的一致化处理,暂且不管是如何进行的,我们只需要基于一致的引擎入口进行开发就好了,

对于cocos2d-x引擎的入口我们定义为AppDelegate.h和AppDelegate.cpp看这里面的方法有三个,这几个方法会在各平台应用程序状态改变时候自动回调。

而状态的改变在不同平台是有差异的,cocos2d-x定义cocos2d::CCApplication类统一了各平台的差异,在cocos2d-x中只有1个窗口在运行(符合移动平台,但pc平台原本不是这样),窗口在分配空间,加载完成,被覆盖,被恢复时候会自动回调CCApplication中的函数,因此CCApplication在不同的平台,有不同的实现cpp,(有兴趣同学可以阅读cocos2ds/platform下面的源代码,从cocos2d::CCApplication.h开始)

#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_

#include "cocos2d.h"

class  AppDelegate : private cocos2d::CCApplication
{
public:
    AppDelegate();
    virtual ~AppDelegate();

     virtual bool applicationDidFinishLaunching();//窗口启动完成

      virtual void applicationDidEnterBackground();//窗口进入后台

      virtual void applicationWillEnterForeground();//窗口恢复
};

#endif // _APP_DELEGATE_H_

----------------------------------------------------------------------------------------------------------------------------------

在这几个方法中,就可以做我们要做的事情了。

applicationDidFinishLaunching启动完成

     加载游戏,播放音乐

applicationDidEnterBackground进入背景

   音乐暂停,游戏暂停

applicationWillEnterForeground恢复窗口

 音乐继续,游戏继续

可能,你会想,如果应用退出,我想保留游戏数据如何?

试者找找ccApplication中还有哪些方法。

关注blog.csdn.net/sdhjob @weibo.com/shunfengche

--------------------------------------------------------------------------------------------------------------------------

bool AppDelegate::applicationDidFinishLaunching() {
     CCDirector *pDirector = CCDirector::sharedDirector(); //初始化导演,他是引擎的老大

    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());//绑定opengles窗口,可见,我们可以自定义openGLView
    TargetPlatform target = getTargetPlatform();   //获取当前平台,这个功能不错
      if (target == kTargetIpad)
    {
        CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd"); //不同平台可以指定不同资源目录
        CCEGLView::sharedOpenGLView()->setDesignResolutionSize(960, 640, kResolutionNoBorder);
    }
    else if (target == kTargetIphone)
    {
        if (true == CCDirector::sharedDirector()->enableRetinaDisplay(true)) //大图资源适配
        {
            CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");
        }
        else
        {
            CCFileUtils::sharedFileUtils()->setResourceDirectory("iphone");
        }
    }
    else
    {
        // android, windows, blackberry, linux or mac
        CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");
        CCEGLView::sharedOpenGLView()->setDesignResolutionSize(960, 640, kResolutionNoBorder);
    }

    pDirector->setDisplayStats(true);//是否显示FPS ( 每秒绘制多少帧,最高60)

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);  //设置FPS 在cocos2d-x 启动后内部封装了FPS的逻辑,虽然helloWorld图片没变化,但其实一直在重绘。

    // 创建一个场景
    CCScene *pScene = HelloWorld::scene();

    // 显示这个场景到窗口,必然所有的绘制在场景中定义的
    pDirector->runWithScene(pScene);

    return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init(); 

    static cocos2d::CCScene* scene();
    void menuCloseCallback(CCObject* pSender);

    void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HelloWorld实际上是一个图层CCLayer,不过CCScene和CCLayer都继承CCNode,所以所有显示在窗口的东西都是CCNode,CCNode通过二阶段构造来初始,所以都有init方法,我们只要记住,在这里完成初始定义图层的资源。

bool HelloWorld::init()
{
    //////////////////////////////
    if ( !CCLayer::init() )    {
        return false;
    }
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); //获取窗口大小
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();    //获取窗口方向

    /////////////////////////////
    //添加一个图片菜单层(所有Node都可以addChild)
    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)); //将菜单定位到窗口右下角 (0,0点在哪?,猜下)

    // 创建菜单                                              

    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);

    //////////////////////////////下面3行添加整个图片
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    this->addChild(pSprite, 0);
   
    // 有了这句该图层会接收事件回调ccTouchesBegan,如果想捕获抬起事件,如何做?
    this->setTouchEnabled(true);
   
    return true;
}

/////////////////////////////////////////////////////////////////////////////////按下Menu回调

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

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

/////////////////////////////////////////////////////////////////////////////////在Layer按下将回调

void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
    CCTouch* touch = (CCTouch*)(* pTouches->begin());
    CCPoint pos = touch->getLocation();
   
    CCLog("touch, x = %f, y = %f", pos.x, pos.y);
}

最后这个 Layer如何显示到屏幕的呢?

   // 创建一个场景
    CCScene *pScene = HelloWorld::scene();

    // 显示这个场景到窗口,必然所有的绘制在场景中定义的
    pDirector->runWithScene(pScene);

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();  //貌似和1.x版本有所不同,注意了。
     // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

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

    // return the scene
    return scene;
}

在scene方法中创建了一个CCScene对象并把HelloWorldLayer 添加到场景了。