cocos2dx3.2helloword分析

前端之家收集整理的这篇文章主要介绍了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
}
原文链接:https://www.f2er.com/cocos2dx/346413.html

猜你在找的Cocos2d-x相关文章