新手学习cocos2d-x

10 篇文章 0 订阅
7 篇文章 0 订阅

cocos2d-x更新很快,笔者刚开始学习cocos2d-x的时候是3.0版本,现在3.5正式版已经发布了。在她的3.0版本以后,加强了3D方面的功能。都说cocos2d-x是做2D游戏的,Unity3D是做3D的,可是发展这么快以后什么格局谁知道呢,以下带来cocos2d-x3.0版本最基本的代码解释,笔者当初学的时候也是硬着头皮学的,因为C++的基础不太好。

HelloWorld中包含AppDelegate和HelloWorld两个cpp文件和对应的头文件:

其中AppDelegate.h如下:

#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_
(此处是定义AppDelegate的宏,用ifndef的原因是检查以前是否被定义过,防止多次包含)
#include "cocos2d.h"
class  AppDelegate : private cocos2d::Application(子类AppDelegate派生于基类cocos2d::Application,继承方式是private)
{
public:
    AppDelegate();
    virtual ~AppDelegate();
(声明AppDelegate函数接口,以及析构函数)

    virtual bool applicationDidFinishLaunching();(游戏启动时调用的函数,作用是初始化导演类Director以及场景对象)
    virtual void applicationDidEnterBackground();(游戏进入后台运行时的调用的函数)
    virtual void applicationWillEnterForeground();(游戏进入前台时候调用的函数)
};
#endif // _APP_DELEGATE_H_(宏结束)

AppDelegate.cpp代码如下:

#include "AppDelegate.h"

#include "HelloWorldScene.h"


USING_NS_CC;
(此处声明了命名空间,类似于C++中的using namespace std; 这里是using namespace cocos2d;  cocos2d-x中定义了很多宏非常方便)

AppDelegate::AppDelegate()

{

}

AppDelegate::~AppDelegate() 
{
}

bool AppDelegate::applicationDidFinishLaunching() {
    auto director = Director::getInstance(); (游戏启动时初始化导演类Director)
    auto glview = director->getOpenGLView();(至函数结速是配置OpenGL视图,命名为"My Game")
    if(!glview) {
        glview = GLView::create("My Game");
        director->setOpenGLView(glview);
    }

    director->setDisplayStats(true);

(设置是否显示游戏运行中的帧率FPS,当前帧绘制定点坐标数GL vert,渲染数目GL Calls,渲染每帧所用的时间,设置true是显示否则为false)

    director->setAnimationInterval(1.0 / 60);

    auto scene = HelloWorld::createScene();

(创建HelloWorld场景)

    director->runWithScene(scene);
(进入该场景)
    return true;
}

void AppDelegate::applicationDidEnterBackground() (游戏进入后台运行)

{

    Director::getInstance()->stopAnimation();(停止游戏中的动画,Animation动画)
    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();(暂停背景音乐)
}


// this function will be called when the app is active again

void AppDelegate::applicationWillEnterForeground() (游戏进入前台)

 {

    Director::getInstance()->startAnimation();(开始动画)


    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();(继续背景音乐)

}

cocos2d中有很多类似于 auto scene = HelloWorld::createScene();的这种语法,这是静态构造函数。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值