这篇博客主要是带领大家一起了解整个游戏的执行过程,其中涉及的一些譬如导演,场景,层之类的概念将会在后面讲解。

看main函数的区别:

#import <UIKit/UIKit.h>

// Under iOS and the Simulator, we can use an alternate Accelerometer interface
#import "AccelerometerSimulation.h"
//程序入口
int main(int argc, char *argv[]) {
   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//注意这时方法的第四个参数是AppController而不再是AppDelegate了,此时把AppController就是以前的
AppDelegate,AppController继承的还是UIApplicationDelegate,而以前的AppDelegate被用作是游
戏的代理类了,而AppDelegate继承的是cocos2d::Application
    int retVal = UIApplicationMain(argc, argv, nil, @"AppController");
    [pool release];
    return retVal;
}
 再看AppController.mm

#import "AppController.h"
#import "EAGLView.h"
#import "cocos2d.h"
#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppController

#pragma mark -
#pragma mark Application lifecycle

// cocos2d application instance 游戏的单例实例
static AppDelegate s_sharedApplication;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
   
    // Override point for customization after application launch.

// Add the view controller's view to the window and display.
    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
   
    // Init the CCEAGLView 初始化eaglView
    CCEAGLView *__glView = [CCEAGLView viewWithFrame: [window bounds]
                                     pixelFormat: kEAGLColorFormatRGB565
                                     depthFormat: GL_DEPTH24_STENCIL8_OES
                              preserveBackbuffer: NO
                                      sharegroup: nil
                                   multiSampling: NO
                                 numberOfSamples: 0];

// Use RootViewController manage CCEAGLView
    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    viewController.wantsFullScreenLayout = YES;
    //eaglView继承UIView,将eaglView设置为跟控制器的view
    viewController.view = __glView;

// Set RootViewController to window
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // warning: addSubView doesn't work on iOS6
        [window addSubview: viewController.view];
    }
    else
    {
        // use this method on ios6
        [window setRootViewController:viewController];
    }
   
    [window makeKeyAndVisible];
   
    [[UIApplication sharedApplication] setStatusBarHidden:true];
    //游戏开始运行
    cocos2d::Application::getInstance()->run();

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    /*
    
Sent when the application is about to move from active to inactive
state. This can occur for certain types of temporary interruptions (such
as an incoming phone call or SMS message) or when the user quits the
application and it begins the transition to the background state.
    
Use this method to pause ongoing tasks, disable timers, and throttle
down OpenGL ES frame rates. Games should use this method to pause the
game.
     */
       //暂停游戏
    cocos2d::Director::getInstance()->pause();
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
    
Restart any tasks that were paused (or not yet started) while the
application was inactive. If the application was previously in the
background, optionally refresh the user interface.
     */
       //恢复游戏
    cocos2d::Director::getInstance()->resume();
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
    
Use this method to release shared resources, save user data, invalidate
timers, and store enough application state information to restore your
application to its current state in case it is terminated later.
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
       //游戏已经进入后台
    cocos2d::Application::getInstance()->applicationDidEnterBackground();
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
    
Called as part of  transition from the background to the inactive
state: here you can undo many of the changes made on entering the
background.
     */
       //游戏将要进入前台
    cocos2d::Application::getInstance()->applicationWillEnterForeground();
}

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
}

#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}

- (void)dealloc {
    [window release];
    [super dealloc];
}

@end
接下来是AppDelegate.cpp

#include "AppDelegate.h"
#include "HelloWorldScene.h"

USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()
{
}

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director  初始化导演,EGLView
    auto director = Director::getInstance();
    auto eglView = EGLView::getInstance();

//设置导演的openglView
    director->setOpenGLView(eglView);
   
    // turn on display FPS 显示刷帧状态,就是屏幕左下角的那个不断变化的数字
    director->setDisplayStats(true);

// set FPS. the default value is 1.0/60 if you don't call this 设置刷帧频率 1.0/60表示每秒刷新屏幕60次
    director->setAnimationInterval(1.0 / 60);

// create a scene. it's an autorelease object  创建一个自动释放的场景
    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::sharedEngine()->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::sharedEngine()->resumeBackgroundMusic();
}
下面是HelloWorldScene.cpp

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object 创建一个自动释放的场景
    auto scene = Scene::create();
   
    // 'layer' is an autorelease object 创建一个自动释放的层,create方法在HelloWorldScene.h中用宏定义声明了: CREATE_FUNC(HelloWorld);
    auto layer = HelloWorld::create();

// add layer as a child to scene 将层加入场景
    scene->addChild(layer);

// return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
   
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point 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 创建一个自动释放的菜单项,包括正常图片,选中图片,回调函数,回调代理类
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    //设置菜单项的位置,注意cocos2d的原点在左下角,向右为X正方向,向上为Y正方向
    closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

// create menu, it's an autorelease object 创建一个菜单,包括菜单项
    auto menu = Menu::create(closeItem, NULL);
    //设置菜单位置
    menu->setPosition(Point::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("Hello World", "Arial", 24);
   
    // position the label on the center of the screen 设置位置
    label->setPosition(Point(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" 创建一个精灵,包括精灵图片
    auto sprite = Sprite::create("HelloWorld.png");

// position the sprite on the center of the screen 设置精灵图片
    sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

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

//菜单项回调事件
void HelloWorld::menuCloseCallback(Object* pSender)
{
    //导演运行结束
    Director::getInstance()->end();
//如果是ios平台就退出程序
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

cocos2d-x 第二篇 HelloWorld的流程的更多相关文章

  1. Activiti第二篇【管理流程定义、执行任务和流程实例、流程变量】

    上篇Activiti只是一个快速入门案例,这篇就讲定义.部署.查看任务等等的一些细节[涉及到的数据库表.对象等等]- 管理流程定义 管理流程定义主要涉及到以下的4张表: -- 流程部署相关的表 SEL ...

  2. Tomcat 第二篇:启动流程

    1 Tomcat 基本知识 首先介绍一些 Tomcat 的基本知识,防止有纯小白看的云里雾里的. 下面这张图是一个下载好二进制版的的 Tomcat ,直接解压得到的,虽然版本是 9.x ,但是这个目录 ...

  3. RabbitMQ学习总结 第二篇:快速入门HelloWorld

    目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...

  4. 第二篇 界面开发 (Android学习笔记)

    第二篇 界面开发 第5章 探索界面UI元素 ●The Android View Class     ●△Widget设计步骤 需要修改三个XML,以及一个class: 1)第一个xml是布局XML文件 ...

  5. [转]Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇--开发工具库篇,主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多 ...

  6. Python开发【第二篇】:初识Python

    Python开发[第二篇]:初识Python   Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏 ...

  7. 第二篇 :微信公众平台开发实战Java版之开启开发者模式,接入微信公众平台开发

    第一部分:微信公众号对接的基本介绍 一.填写服务器配置信息的介绍 登录微信公众平台官网后,进入到公众平台后台管理页面. 选择 公众号基本设置->基本配置 ,点击“修改配置”按钮,填写服务器地址( ...

  8. Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇——开发工具库篇,**主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容 ...

  9. 第二篇 SQL Server代理作业步骤和子系统

    本篇文章是SQL Server代理系列的第二篇,详细内容请参考原文. SQL Server代理作业由一系列的一个或多个作业步骤组成.一个作业步骤分配给一个特定的作业子系统(确定作业步骤去完成的工作). ...

随机推荐

  1. spring配置文件详解--真的蛮详细

    spring配置文件详解--真的蛮详细   转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...

  2. codevs4096 删数问题

    题目描述 Description 键盘输入一个高精度的正整数N,去掉其中任意S个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对给定的N 和S,寻找一种方案使得剩下的数字组成的新数最小. 输入 ...

  3. [AngularJS] 常用指令

    常用指令 ng-hide指令,用于控制部分HTML元素可见(ng-hide="false")和不可见状态(ng-hide="true"),如下: <div ...

  4. myeclipse的项目导入到eclipse下,com.sun.org.apache.commons.beanutils.BeanUtils不能导入

    com.sun.org.apache.commons.beanutils.BeanUtils这个包不能引入了怎么办自己下了个org.apache.commons的jar包了之后,改成import or ...

  5. BeanFactory和ApplicationContext的区别

     1.BeanFactory和ApplicationContext的异同点: 相同点:     两者都是通过xml配置文件加载bean,ApplicationContext和BeanFacotry相比 ...

  6. JSP自定义标签之Hello Costom tag小例子

    1.项目结构 2.实现自定义tag所需依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId ...

  7. IO基础

    流向: 输入流:从硬盘到java 程序 (读数据) 输出流:从java 到硬盘 (写数据) 数据类型: 字节流:    {用记事本打开,不能读懂,用 字节流}     输入:   InputStrea ...

  8. BZOJ1503——郁闷的出纳员

    1.题目大意:一道treap题,支持插入,询问第K大,还有全体修改+上一个值,如果某个点值小于x,那么就删除这个点 插入100000次,询问100000次,修改100次..最后输出删了多少个点 2.分 ...

  9. 从Objective-C转战C++ Android平台开发实践(C++/Java)

    是否使用虚拟方法 最好在不用“virtual”关键字的情况下声明所有cpp成员方法 但是在写CPP头文件时,请检查有没有父类的方法被当前的工作覆盖.如果有,请确保将这些方法改为虚拟方法. 如果从父类继 ...

  10. BestCoder Round #61 1002 Game

    Problem Description XY is playing a game:there are N pillar in a row,which numbered from 1 to n.Each ...