---
title: "cocos2d Loading的处理"
author: "alex09"
date: "2011-07-11T12:42:44Z"
tags: []
categories: ["移动开发"]
slug: "cocos2d-loading"
draft: false
lastmod: "2011-07-11T12:42:44Z"
id: 1121550
url: "https://alex09.iteye.com/blog/1121550"
---

```java
(id) init
{
    self = [super init];
    if ( self )
    {   
        // Add the Label (in cocos2D universe)

        BitmapFontAtlas *label = [bitmapfontatlas 
         bitmapfontatlaswithstring: @"initializing game" 
         fntfile: @"comic_atlas.fnt" 
         alignment: uitextalignmentcenter];

        label.position = ccp(240, 160);

        label.opacity = 0;

        [self addchild: label z: 0 tag: kinitializinglabel];

        // Add the UIActivityIndicatorView (in UIKit universe)

        self.activityIndicatorView = [[[uiactivityindicatorview alloc] 
          initWithActivityIndicatorStyle: 
          UIActivityIndicatorViewStyleWhiteLarge] autorelease];

        activityIndicatorView.center = ccp(190,240);

        [activityindicatorview startanimating];

        [[self battleview] addSubview: activityIndicatorView];

        [self schedule: @selector(loadinginit)]；

        // At this point, the cocos2d rendering is fine.
    }
    return self;
}

- (void) loadingInit
{
    [self unschedule: @selector(loadinginit)];

    // Fade in nicely
    BitmapFontAtlas *label = (BitmapFontAtlas*)[self getchildbytag: kinitializinglabel];
    [label runaction: [fadein actionwithduration: .25]];
	
    [self schedule: @selector(loadingstep0) interval: .25];
}

- (void) loadingStep0
{
    [self unschedule: @selector(loadingstep0)];    

    // Add you own code to load texture, sounds etc...

    // The cocos2D thread will be waiting (since it's the same as 
       the main thread) but the UIKit thread will keep running in parallel
    // Can do multiple steps or just one
    [self schedule: @selector(loadingstep1) interval: .5];
}

- (void) loadingStep1
{
    [self unschedule: @selector(loadingstep1)];

    // Add you own code to load texture, sounds etc...
    [self schedule: @selector(loadingdone) interval: .1];
}

- (void) loadingDone
{
    gameIsReady = YES；
    [self.activityindicatorview removefromsuperview];

    BitmapFontAtlas *label = (BitmapFontAtlas*)[self getchildbytag: kinitializinglabel];
    [label setstring: nslocalizedstring(@"tap to start!", nil)];
    [label runaction: [repeatforever actionwithaction:
                       [sequence actionone: [easeinout actionwithaction: 
                       [moveto actionwithduration: .6 position: ccp(210, 160)] rate: 2] 
                       two: [easeinout actionwithaction: 
                       [moveto actionwithduration: .6 position: ccp(270, 160)] rate: 2]
                        ]       
                       ]
     ]；
}

- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ( gameIsReady )
    {
        [self opengame]；
    }
    return kEventHandled;
}

```


http://stackoverflow.com/questions/1133123/multi-threaded-opengl-programming-in-cocos2d-iphone