cocos2d-x 第二篇 HelloWorld的流程

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

看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
}

相关推荐