Cocos2d-x 3.0正式版 HelloWorld分析

我们不管是对于一款编程语言的学习,还是对一个游戏引擎的学习,第一个基本都是HelloWorld,那么我们现在来分析一下,Cocos3.0正式版的HelloWord  又有哪些改进?


我们可以对比2.2版本的Cocos,现在3.0的HelloWord已经改名为cpp-empty-test。这次我们来分析一下这个cpp-empty-test


运行程序,我们可以看到熟悉的HelloWorld程序:




和之前cocos2d-x2.x版本的HelloWorld看起来没太大差别,主要改变有三点:


标题文字显示为Cpp Empty Test。


按钮由下面改到了上面。


左下角的信息显示有所不同,以前显示的是批次  每帧的平均运行秒数FPS数,现在改成了 OPENGL的顶点数量   OPENGL的批次   FPS数/每帧的平均运行秒数。


说了一堆蛋疼的废话  现在来具体看下工程代码:




我们可以看到Visual Studio工程下目录有两个


分别是:

  Classes:程序中的类。


{
      AppDelegate.h   AppDelegate.cpp:Cocos2d-x程序框架


       AppMacros.h    主要是设置分辩率及对应的资源目录                


       HelloWorldScene.h  HelloWroldScene.cpp   场景显示层


}

win32:WIN32程序的主函数

{
       main.cpp   winMain主函数
}

在WinMain函数中,只有一个实例化程序并运行它的过程:

int APIENTRY _tWinMain(HINSTANCE hInstance,

                       HINSTANCE hPrevInstance,

                       LPTSTR    lpCmdLine,

                       int       nCmdShow)

{

    UNREFERENCED_PARAMETER(hPrevInstance);

    UNREFERENCED_PARAMETER(lpCmdLine);


    // 创建一个程序对象。

    AppDelegate app;

// Run起来。

    return Application::getInstance()->run();

}


 一切,都被封装到程序类AppDelegate中。这是一个基于Cocos2d-x的cocos2d::Application

类的派生类。它将程序框架封装为一个类,提供了统一的多平台上基本程序框架的实现。


AppDelegate.cpp:

#include "AppDelegate.h"

#include <vector>
#include <string>

#include "HelloWorldScene.h"
#include "AppMacros.h"

USING_NS_CC;
using namespace std;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate() 
{
}

//初始化函数
bool AppDelegate::applicationDidFinishLaunching() {
    // 取设备
    auto director = Director::getInstance();
	// 取OpenGL
    auto glview = director->getOpenGLView();
    if(!glview) {
	    //如果为空,创建 Cpp Empty Test的窗口。
        glview = GLView::create("Cpp Empty Test");	
	    //设置设备使用的窗口,这一句可以去掉。
        director->setOpenGLView(glview);
    }
     //设置设备使用的窗口。
    director->setOpenGLView(glview);

    // 是WP8平台,设置分辩率
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	// 在WP8上跑DX11,使用ResolutionPolicy::NO_BORDER模式设置分辩率会有一个BUG,这里改为ResolutionPolicy::SHOW_ALL模式。  

    glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::SHOW_ALL);
#else
    glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
#endif

	//取得了视窗的大小
	Size frameSize = glview->getFrameSize();
    
    vector<string> searchPath;

    //根据视窗大小与分辩率的大小选择相应的资源目录。

    //ipadhd
	if (frameSize.height > mediumResource.size.height)
	{
        searchPath.push_back(largeResource.directory);

        director->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
	}
    //ipad
    else if (frameSize.height > smallResource.size.height)
    {
        searchPath.push_back(mediumResource.directory);
        
        director->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));
    }
    //iphone
	else
    {
        searchPath.push_back(smallResource.directory);

        director->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
    }
    
    // 设置资源目录
    FileUtils::getInstance()->setSearchPaths(searchPath);
	
    // 打开FPS显示
    director->setDisplayStats(true);

    // 设置每秒60帧
    director->setAnimationInterval(1.0 / 60);

    // 创建HelloWorld场景
    auto scene = HelloWorld::scene();

    // 运行场景
    director->runWithScene(scene);

    return true;
}

// 当打电话时,游戏跳到后台,响应这句
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();

    // 如果使用声音文件,下面可以用这句代码暂停
    // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// 当电话打完以后,选择恢复游戏时,响应这句
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

    // 如果使用声音文件,下面可以用这句代码恢复
    // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}


代码跟之前版本差别不大,要注意的是,cocos2d-x 3.0版本使用了auto自动类型变量,这个是C++11的新标准,比如原来要指定变量是int  float  double char,现在可以用auto,在赋值的时候,编译器会自动识别类型。  很方便吧



        下面我们来看一下HelloWorld场景,它是一个基于cocos2d::Layer的派生类。cocos2d::Layer是什么?在这里,打个比方来建立一些基本的认知,比方说我们日常生活拍一部电影。从Cocos2d-x的框架体系来看,director是一个电影的导演   比如在北京拍  北京就是一个Scene   在王府井取一个景  那个王府井就是一个Layer  在王府井拍一个景的话 我们需要演员  也就是Sprite   演员需要有一些肢体语言的动作 也就是我们的Action   这样 相信大家也有一定的了解了吧。


        一个程序要想表现出精彩的世界,要先建立一个Scene,然后增加Layer,然后在这些Layer上增加相应的人。而我们站在Layer上 也会跟着一起运动也就形成了Cocos中的Action。


  OK,现在我们来看一下如何创建Scene和Layer:

HelloWorldScene.h:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    // 初始化
    virtual bool init();  

    // 静态函数创建Scene
    static cocos2d::Scene* scene();
    
    // 响应按钮退出程序
    void menuCloseCallback(Ref* sender);
    
    // 增加一个静态的create函数来创建实例。
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__
   
	HelloWorldScene.cpp:

#include "HelloWorldScene.h"
#include "AppMacros.h"
//使用Cocos2d-x命名空间
USING_NS_CC;
//静态函数创建场景
Scene* HelloWorld::scene()
{
    // 创建一个Scene,即刚才电影提到的北京
    auto scene = Scene::create();
    
    // 创建一个Layer,即王府井
    HelloWorld *layer = HelloWorld::create();

    // 将王府井这一个层放到北京这个大场景中
    scene->addChild(layer);

    return scene;
}

// 初始化
bool HelloWorld::init()
{
    //先进行初始化
    if ( !Layer::init() )
    {
        return false;
    }
    //取得分辩率的大小及原点坐标
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto origin = Director::getInstance()->getVisibleOrigin();

    // 创建一个菜单项,它由两张图片来表现普通状态和按下状态,设置按下时调用menuCloseCallback函数响应关闭
    auto closeItem = MenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
    
    closeItem->setPosition(origin + Point(visibleSize) - Point(closeItem->getContentSize() / 2));

    //由菜单项创建菜单.
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu, 1);
    
    //创建一个文字标签
    auto label = LabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);
    
    // 设置居中显示
    label->setPosition(Point(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    // 将文字标签放到当前Layer中。
    this->addChild(label, 1);

    // 增加一个图片精灵
    auto sprite = Sprite::create("HelloWorld.png");

    // 设置居中显示
    sprite->setPosition((ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    //将Sprite放到当前Layer中。
    this->addChild(sprite);
    return true;
}

//响应菜单按下时的事件处理
void HelloWorld::menuCloseCallback(Ref* sender)
{
	//如果是WP8平台,弹出消息框提示一下。
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif
	//否则,终止程序。
    Director::getInstance()->end();
	//退出程序
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

Layer中增加了Sprite(精灵),Menu(按钮),LabelTTF(文字)等表现物,有了这些表现物,一个Layer(层)才有价值。


好了  学习的时间总是过的很快,珍惜现在,展望未来,我们下次再见:)


By:Net Fly


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值