Cocos2d-x学习笔记(1)

Cocos2d-x原型Cocos2d,基于Cocos2d-iPhone,跨平台。

Hello Workd分析:

1、“resource”目录

该目录主要用于存放游戏中须要的图片、音频和配置等资源文件。可在当中创建子目录。

“resource”目录能够为默认游戏执行时目录。

2、“include”和“source”目录

这两个目录用于放置游戏头文件和源码文件。项目模板中加入的main.h、main.cpp、resource.h是平台相关的。为Windows专有。

3、"AppDelegate.h“和”AppDelegate.cpp“

这个两个文件是Cocos2d-x游戏的通用入口文件。类似于Windowsproject中主函数所在的文件。打开”AppDelegate.cpp“能够看到系统自己主动加入的代码。实现了AppDelegate类,这个类控制着游戏的生命周期。除去构造函数和析构函数外,共同拥有三个方法。

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director启动应用程序后将调用这种方法。默认的实现中已经包括了游戏启动后的必要准备
    CCDirector* pDirector = CCDirector::sharedDirector();//初始化游戏引擎控制器CCDirector。以便启动游戏引擎
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);

    // turn on display FPS启用FPS显示
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this设置画图间隔。人眼的刷新频率为1/60秒。
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object创建一个场景
    CCScene *pScene = HelloWorld::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();

    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {//该方法与上个方法成对出现,应用程序返回前台时被调用。
    CCDirector::sharedDirector()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

在第一个函数中能够加入pDirector->enableRetinaDisplay(true)用于开启高分辨率屏幕。

在HelloWorldScene.h与HelloWorldScene.cpp中定义了HelloWorld项目中默认的游戏场景。Cocos的游戏结构能够简单的概括为场景、层、精灵。HelloWorld类继承于CCLayer,因此HelloWorld本身是一个层,HelloWorld类包括一个静态函数和两个实例方法。

以下介绍一下:

(1)static cocos2d::CCScene* scene();是CCLayer的一个子类。能够在子类中加入各种精灵和逻辑处理代码。

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();//创建场景

    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();//创建子类层

    // add layer as a child to scene
    scene->addChild(layer);将子类层加到主场景

    // return the scene
    return scene;
}

(2)bool init()初始化HelloWorld类

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first对父类进行初始化
    if ( !CCLayer::init() )
    {
        return false;
    }

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();//
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();//

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.
    //创建菜单并加入到层
    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    //设置关闭button位置
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

    /////////////////////////////
    // 3. add your codes below...
    //创建“Hello World”标签并加入到层中
    // add a label shows "Hello World"
    // create and initialize a label

    CCLabelTTF* pLabel = CCLabelTTF::create("Yu xikuo", "Arial", 24);//创建的是yuxikuo的标签

    // position the label on the center of the screen设置标签的位置
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    // add the label as a child to this layer创建的label到层中
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"加入HelloWorld.png精灵到层中
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen设置精灵在层中的位置
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);将精灵加入到层

    return true;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-07-31 03:02:32

Cocos2d-x学习笔记(1)的相关文章

Cocos2D学习笔记(1)- 常用的类

1.坐标系 >屏幕坐标系(UIKit):原点在左上角! >OpenGl坐标系:原点在屏幕的左下角! 2.游戏设计:Director--Scene--Layer--Sprite. >CCDirector:导演类,相当于是游戏策划,负责整个游戏的布局和运行规则的制定. >CCScene:场景类,每个场景可以是一个界面或一个关卡. >CCLayer:图层类,为了方便游戏界面的渲染管理. >CCSprite:精灵类, 小结:一个导演类(CCDirector)可以指挥多个场景类(

cocos2d-js引擎学习笔记

Scale9Sprite 在用Scale9Sprite.create的时候出现Uncaught TypeError: Cannot call method 'create' of undefined这个错误, 后来发现在默认情况下,project.json里的modules只自带cocos2d模块,通过检查frameworks/cocos2d-html5/moduleConfig.json,可以看到cocos2d模块里并没有CCScale9Sprite.js这个类. 它在GUI里,所以可以在mo

cocos2dx游戏开发学习笔记2-从helloworld开始

一.新建工程 具体安装和新建工程的方法在cocos2dx目录下的README.md文件中已经有详细说明,这里只做简单介绍. 1.上官网下载cocos2dx-3.0的源码,http://www.cocos2d-x.org/ 2.安装python2.7 3.运行setup.py安装 4.执行cocos new helloworld -p helloworld -l cpp,生成新工程 二.新建工程中包含的东西 -Classes AppDelegate.cpp      -----游戏真正开始执行的地

《Cocos2d-x游戏开发实战精解》学习笔记3--在Cocos2d-x中播放声音

<Cocos2d-x游戏开发实战精解>学习笔记1--在Cocos2d中显示图像 <Cocos2d-x游戏开发实战精解>学习笔记2--在Cocos2d-x中显示一行文字 之前的内容主要都是介绍如何在屏幕上显示图像,事实上除了图像之外,音乐的播放也可以被理解为一种显示的方式,本节将学习在Cocos2d-x中播放声音的方法. (1)在HelloWorld.h中对HelloWorld类进行如下定义: class HelloWorld : public Cocos2d::Layer { pu

cocos2dx游戏开发&mdash;&mdash;微信打飞机学习笔记(三)&mdash;&mdash;WelcomeScene的搭建

一.场景与层的关系: cocos2dx的框架可以说主要由导演,场景,层,精灵来构成: 1.其中导演,意如其名,就是操控整个游戏的一个单例,管理着整个游戏. 2.场景就像电影的一幕剧情,所以说,懂得如何划分好游戏的场景,是开始动手做游戏的第一步. 3.一个场景会有很多层,用来处理场景不同的功能. 4.而精灵则是最小的单位,比如子弹,飞机,敌机都是一个个的精灵所组成的.   二.WelcomeScene的搭建: 1.场景和层的二种搭建方法: (1)一种就是跟HelloWorld示例一样的方法,以一个

Cocos2d-x 3.2 学习笔记(五)Sprite Node

游戏中最重要的元素Sprite精灵,关于精灵的创建,精灵的控制等等. 涉及到的类Class: AnimationFrame 动画帧. Animation 动画对象:一个用来在精灵对象上表现动画的动画对象. AnimationCache 动画缓存单例类. 如何你想要保存动画,你需要使用这个缓存. Sprite 精灵;定义为二维图像. SpriteBatchNode 与批量节点类似,如果包含子节点会在一次OpenGL调用内绘制完成. SpriteFrame 一个精灵帧. SpriteFrameCac

【Cocos2D-X 学习笔记】为精灵添加单点触控

由于Cocos2d-x处于新学的阶段,因此最近也无法进行系统地更新,只会选择一些典型的Demo贴上来,一来是与大家分享,而来也可以作为以后回顾时的参考. 今天介绍一下Cocos2d-x的触摸事件处理,了解Android开发的朋友们知道,Android里会用一个OnClickListener()进行事件监听,而在J2SE中也会有Event类实现专门的监听处理.在Cocos2d-x中,因为是游戏引擎,用户在玩游戏时总是要通过屏幕与游戏进行交互,可想而知触摸事件是主要处理的事件.这里主要讲一下如何为精

Cocostudio学习笔记(3) ImageView + Slider

这篇记录了两个控件的使用流程:ImageView 和 Slide. ---------------------------------------------------------------------------------------------------------------------------- ImageView ---------------------------------------------------------------------------------

Cocos2dx 学习笔记整理----场景切换

据说Cocos2dx场景切换的方法有32种:cocos2dx 常见的32种切换场景的动画 无需一一求证,只需要知道切换场景需要怎么做就行了. 作为导演CCDirector,切换场景的事情当然归它管了. 切换场景的接口如下: ? 1 CCDirector::sharedDirector()->replaceScene(cocos2d:CCScene * pScene); 所以,我们只要把需要切换的场景实例传进去就可以了. ? 1 2 CCScene * pScene = GameMain::sce

cocos2dx游戏开发——微信打飞机学习笔记(一)——开发准备

一.环境的搭建 1.Windows开发准备: (1)软件下载及安装 •下载Cocos2d-x 最新版本:http://www.cocos2d-x.org/download 或者从Cocos2d-x GitHub主页中克隆Develop分支:https://github.com/cocos2d/cocos2d-x •配置Python 2.7 环境:http://www.python.org/download/releases/ •建议IDE:Visual Studio 2013 •运行cocos2