cocos2dx3.2helloword分析

废话不说  直接看注释
<span style="font-size:24px;color:#ff0000;">AppDelegate.cpp</span>
#include "AppDelegate.h"
#include "HelloWorldScene.h"

//USING_NS_CC == using namespace cocos2d
USING_NS_CC;//using namespace cocos2d

//构造函数
AppDelegate::AppDelegate() {

}
//析构函数
AppDelegate::~AppDelegate()
{
}

//程序初始化函数
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
	//获取设备
    auto director = Director::getInstance();
	//取得OpenGL窗口
    auto glview = director->getOpenGLView();
    if(!glview) {
		//如果为空,则创建以" www.sundaboke.com"为窗口标题的窗口。
        glview = GLView::create("www.sundaboke.com");//这里我修改过 //刚刚开始是My Game 我修改成www.sundaboke.com  cocos2dx 默认的设置的窗口大小是960,640
		//设置设备使用的窗口,此句可以去掉。
		//director->setOpenGLView(glview);这一句我个人医保写在下面
    }
	//设置设备使用的窗口,
	director->setOpenGLView(glview);

    // turn on display FPS
	//打开FPS显示
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
	//设置每秒60帧
    director->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
	//创建HelloWorld场景
    auto scene = HelloWorld::createScene();

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

    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() {
    Director::getInstance()->stopAnimation();

	//如果使用使用,下面可以用这句代码暂停
    // 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();
}
<span style="font-size:48px;color:#ff0000;"></span><pre name="code" class="cpp"><span style="font-size:48px;color:#ff6666;">HelloWorldScene.h</span>

<pre name="code" class="cpp">#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
	//静态函数创建createScene    cocos2dx3.x以后都是这个
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
	//初始化
    virtual bool init();  

    // a selector callback
	//响应按钮退出程序
    void menuCloseCallback(cocos2d::Ref* pSender);

    // implement the "static create()" method manually
	// 增加一个静态的create函数来创建实例。
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp


#include "HelloWorldScene.h"

USING_NS_CC;//使用cocos2dx命名空间 USING_NS_CC == using namespace cocos2d

//静态函数创建场景
Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
<span style="white-space:pre">	</span>//创建一个Scene 
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
<span style="white-space:pre">	</span>//创建一个 layer
    auto layer = HelloWorld::create();

    // add layer as a child to scene
<span style="white-space:pre">	</span>// 把 layer 发到 scene 中
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
<span style="white-space:pre">	</span>//先初始化
    if ( !Layer::init() )
    {
        return false;
    }
    //获取屏幕分辨率
    Size visibleSize = Director::getInstance()->getVisibleSize();
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>//获取圆点坐标
    Vec2 origin = Director::getInstance()->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
<span style="white-space:pre">	</span>//创建一个按钮,用2张图表示,默认是第一张图片,按下使用第二张,按下的时候调用HelloWorld::menuCloseCallback方法
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    //设置按钮的位置
<span style="white-space:pre">	</span>closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
<span style="white-space:pre">	</span>//由菜单项创建按钮.  
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);//设置菜单的位置
    this->addChild(menu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    //创建一个文本标签,
    auto label = LabelTTF::create("www.sundaboke.com", "Arial", 24);
    
    // position the label on the center of the screen
<span style="white-space:pre">	</span>//设置文本标签的位置
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(label, 1);//在屏幕上面显示标签

    // add "HelloWorld" splash screen"
<span style="white-space:pre">	</span>//创建一个背景
    auto sprite = Sprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
<span style="white-space:pre">	</span>//设置背景的位置
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
<span style="white-space:pre">	</span>//把背景添加到屏幕中
    this->addChild(sprite, 0);
    
    return true;
}

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

<span style="white-space:pre">	</span>//退出程序
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
时间: 2025-01-08 10:18:59

cocos2dx3.2helloword分析的相关文章

Cocos2d-x3.3RC0的Android编译Activity启动流程分析

本文将从引擎源代码Jni分析Cocos2d-x3.3RC0的Android Activity的启动流程,以下是具体分析. 1.引擎源代码Jni.部分Java层和C++层代码分析 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXV4aWt1b18x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > watermark/2/text/aHR0cDov

Cocos2d-X3.0 刨根问底(九)----- 场景切换(TransitionScene)源代码分析

上一章我们分析了Scene与Layer相关类的源代码,对Cocos2d-x的场景有了初步了解,这章我们来分析一下场景变换TransitionScene源代码. 直接看TransitionScene的定义 class CC_DLL TransitionScene : public Scene { public: /** Orientation Type used by some transitions */ enum class Orientation { /// An horizontal or

Cocos2d-X3.0 刨根问底(九)----- 场景切换(TransitionScene)源码分析

上一章我们分析了Scene与Layer相关类的源码,对Cocos2d-x的场景有了初步了解,这章我们来分析一下场景变换TransitionScene源码. 直接看TransitionScene的定义 class CC_DLL TransitionScene : public Scene { public: /** Orientation Type used by some transitions */ enum class Orientation { /// An horizontal orie

Cocos2d-X3.0 刨根问底(七)----- 事件机制Event源码分析

这一章,我们来分析Cocos2d-x 事件机制相关的源码, 根据Cocos2d-x的工程目录,我们可以找到所有关于事件的源码都存在放在下图所示的目录中. 从这个event_dispatcher目录中的文件命名上分析 cocos2d-x与事件相关的类一共有四种, Event, EventListener,EventDispatcher, Touch分别为 事件,事件侦听器,事件分发器,触摸 我们先从Event类开始. 打开CCEvent.h文件 /** * Base class of all ki

Cocos2d-x3.1TestCpp之MotionStreakTest Demo分析

1.构成代码 VisibleRect.h VisibleRect.cpp AppDelegate.h AppDelegate.cpp HelloWorldScene.h HelloWorldScene.cpp MotionStreakDemo.h MotionStreakDemo.cpp 2.代码分析 (1)VisibleRect.Appdelegate的代码均为TestCpp提供代码: (2)HelloWorldScene.cpp中只需把上一篇Cocos2d-x3.1TestCpp之NewRe

Cocos2d-X3.0 刨根问底(八)----- 场景(Scene)、层(Layer)相关源码分析

本章节我们重点分析Cocos2d-x3.0与 场景.层相关的源码.这部分源码集中在 libcocos2d –> layers_scenes_transitions_nodes目录下面 我先发个截图大家了解一下都有哪些文件.红色框里面的就是我们今天要分析的文件. 从命名上可以了解,这个文件夹里的文件主要包含了  场景,层,变换这三种类型的文件. 下面我们先分析Scene类 打开CCScene.h文件 /** @brief Scene is a subclass of Node that is us

Cocos2d-X3.0 刨根问底(六)----- 调度器Scheduler类源码分析

上一章,我们分析Node类的源码,在Node类里面耦合了一个 Scheduler 类的对象,这章我们就来剖析Cocos2d-x的调度器 Scheduler 类的源码,从源码中去了解它的实现与应用方法. 直入正题,我们打开CCScheduler.h文件看下里面都藏了些什么. 打开了CCScheduler.h 文件,还好,这个文件没有ccnode.h那么大有上午行,不然真的吐血了, 仅仅不到500行代码.这个文件里面一共有五个类的定义,老规矩,从加载的头文件开始阅读. #include <funct

Cocos2d-X3.0 刨根问底(五)----- Node类及显示对象列表源码分析

上一章 我们分析了Cocos2d-x的内存管理,主要解剖了 Ref.PoolManager.AutoreleasePool这三个类,了解了对象是如何自动释放的机制.之前有一个类 Node经常出现在各种场合,不是做为参数就是做为返回值,那么这一章节我们就去看看这个Node类到底在Cocos2d-x里处于一个什么样的地位. 直接进入主题,我们打开CCNode.h文件.我去,这个文件有1500行,这么长怎么看啊,放松一下整体看过一遍,松了一口气,还好,还没那么糟,文件虽然大,注释占了有90%的篇幅,代

Cocos2dx-3.x 中CCCamera相机类详解及源码分析

Cocos2d-x 3.3版本中加入了相机这个类,该类在3D游戏中是必不可少的,在3D立体游戏中,往往需要视野角度的变化,通过相机的变换才能观察和体验整个游戏世界. CCCamera类基本使用 在游戏中一般有两种类型的相机:一种是透视相机,它在3D游戏中十分常见:另一种是正交相机,它没有透视相机的近大远小的效果而是相机内任何位置的物体大小比例都是一样的. 上图是透视相机的原理图,一般来说,我们通过以下代码创建: _camera = Camera::createPerspective(60, (G