`
myCsdn_taoge
  • 浏览: 38505 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
文章分类
社区版块
存档分类
最新评论

cocos2d-x 学习笔记1

 
阅读更多

helloworld  基础篇(本人初学,未免很多不足,希望多给予批评)

      首先 安装及配置就不细说了,网上有很多参考。先来说说目录结构 resource ,libs ,Classes ,resource 是资源文件夹,存放一些图片等,libs 是存放的类库文件,Classes 类似于java中的编译好类的文件。include 文件中包含 .h 文件 类似于java 中的接口Interface 只是有方法声明,没有具体实现方法,.cpp文件和.h 文件相互对应,及.cpp文件 实现.h文件中声明的方法。如果涉及到多个类之间的方法调用,需要加入 #include "cocoa/CCObject.h" 类似于java的引用相应的类,这样就可以类之间的方法灵活调用。

      下面来说说demo 中的程序入口,其中的main.cpp 跟java的主函数相似,是程序的入口,看看mian.cpp里面的方法

 

#include "main.h"
#include "AppDelegate.h"
#include "CCEGLView.h"

USING_NS_CC;

// uncomment below line, open debug console
// #define USE_WIN32_CONSOLE

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

#ifdef USE_WIN32_CONSOLE
    AllocConsole();
    freopen("CONIN$", "r", stdin);
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);
#endif

    // create the application instance
    AppDelegate app;
    CCEGLView* eglView = CCEGLView::sharedOpenGLView();
    eglView->setFrameSize(480, 320);

    int ret = CCApplication::sharedApplication()->run();

#ifdef USE_WIN32_CONSOLE
    FreeConsole();
#endif

    return ret;
}

 

   可以看到在此类中 涉及到了AppDelegate.cpp 下面看下这个类:

 

#include "cocos2d.h"
#include "CCEGLView.h"
#include "AppDelegate.h"
#include "MapScene.h"
#include "SimpleAudioEngine.h"

using namespace CocosDenshion;

USING_NS_CC;

AppDelegate::AppDelegate()
{
}

AppDelegate::~AppDelegate()
{
    SimpleAudioEngine::end();
}

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 = MapScenes::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();

    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
    CCDirector::sharedDirector()->startAnimation();

    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

 

   如果想运行自己创建的一个类 可以更改此处代码:

   

 // create a scene. it's an autorelease object
    CCScene *pScene = MapScenes::scene();

    // run
    pDirector->runWithScene(pScene);

    把场景设置为自己的类,然后在编译器中将工程设置为默认启动,既可以运行自己创建的类中的方法了! 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics